简体   繁体   中英

Python: FTPLIB Download files with original modify time

I'm trying to write a python script to download files from the FTP server, the below code works for me, but I'm curious is there any way to download the files with the original modify time from FTP.Is that is possible with 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)

As @Justin correctly commented, just set the timestamp after the download.

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()))

Some references:


Though were you downloading all files in a folder, i would be an overkill to call MDTM for each. It that case, you can retrieve the timestamp from the directory listing retrieved using mlsd or `dir. See How to get FTP file's modify time using Python ftplib .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM