我正在尝试编写一个脚本,以便在修改日期小于 24 小时的情况下从 SFTP 服务器检索文件。 这是我迄今为止的尝试:
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)
这是结果...
?rw-rw-rw- 1 0 0 8339 02 Aug 02:42 ENTOUT_709_Payers_20170802024203.csv
如何以 %Y%M%d 的格式检索
"02 Aug 02:42"
?
最佳答案
我有一个替代品:
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
所以你的代码将如下所示:
for attr in s.listdir_attr("ENTOUT_709_Payers_20170802024203.csv"):
print(unix2human(attr.st_mtime))
其中st_mtime是内置的pysftp方法
关于python - pysftp。根据修改日期获取文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45471143/