[英]Pysftp. Get files based on Modification Date
I'm trying to write a script to retrieve files from an SFTP server if the modification date is less than 24 hours. 如果修改日期少于24小时,我正在尝试编写脚本以从SFTP服务器检索文件。 This is my attempt so far: 到目前为止,这是我的尝试:
cnopts = sftp.CnOpts()
cnopts.hostkeys = None
s = sftp.Connection(host=host, username=username, password=password, port=port, cnopts=cnopts)
s.cwd(r"/Outbox")
for attr in s.listdir_attr("ENTOUT_709_Payers_20170802024203.csv"):
print(attr)
Here are the results... 结果如下...
?rw-rw-rw- 1 0 0 8339 02 Aug 02:42 ENTOUT_709_Payers_20170802024203.csv
How would I retrieve "02 Aug 02:42"
in the format of %Y%M%d?
我将如何以%Y%M%d?
的格式检索"02 Aug 02:42"
%Y%M%d?
You can parse the date string you get back from the ftp directory listing like this: 您可以像下面这样分析从ftp目录列表中获取的日期字符串:
import datetime
input_date = "02 Aug 02:42"
parsed_date = datetime.datetime.strptime(input_date,"%d %b %H:%M").replace(year=2017)
print(f"{parsed_date:%Y%m%d}")
Output is 20170802
. 输出为20170802
。
I have a substitute for it: 我可以替代它:
def unix2human(unixtime, fmt = "%Y-%m-%d"):
try:
return datetime.utcfromtimestamp(int(unixtime)).strftime(fmt)
except Exception as e:
log.warning("Failed to convert unixtime string '{}' :: {}".format(unixtime, e))
return None
So your code will look like: 因此您的代码将如下所示:
for attr in s.listdir_attr("ENTOUT_709_Payers_20170802024203.csv"):
print(unix2human(attr.st_mtime))
where st_mtime is the built-in pysftp method 其中st_mtime是内置的pysftp方法
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.