我正在尝试写一个python脚本从FTP服务器下载文件,下面的代码对我有效,但我很好奇是否有任何方法可以从FTP下载原始的modify time文件。这在python中是可能的吗?
Example: FileZilla have a feature to download the files with the original date time
with open(fileName,'wb') as write
def writeData(chunk):
fwrite.write(chunk)
ftp_client.retrbinary('RETR {}'.format(downFileName), writeData)发布于 2021-03-27 20:52:50
正如@Justin正确评论的那样,只需在下载后设置时间戳即可。
remote_path = "/remote/path/foo.txt"
local_path = "/local/path/foo.txt"
timestamp = ftp.voidcmd("MDTM " + remote_path)[4:].strip()
mtime = parser.parse(timestamp)
with open(local_path, "wb") as f:
ftp.retrbinary("RETR " + remote_path, f.write)
os.utime(local_path, (mtime.timestamp(), mtime.timestamp()))一些参考资料:
尽管您下载的是文件夹中的所有文件,但如果我为每个文件都调用MDTM,那就太过分了。在这种情况下,您可以从使用mlsd或` `dir检索的目录列表中检索时间戳。参见How to get FTP file's modify time using Python ftplib。
https://stackoverflow.com/questions/66830670
复制相似问题