【问题标题】:Python: FTPLIB Download files with original modify timePython:FTPLIB 下载具有原始修改时间的文件
【发布时间】:2021-03-27 11:13:13
【问题描述】:

我正在尝试编写一个 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)

【问题讨论】:

标签: python ftp ftplib


【解决方案1】:

正如@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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-27
    • 1970-01-01
    相关资源
    最近更新 更多