I have a python script that downloads a file over FTP using ftplib.
My current download code looks just like the example in the ftp lib docs:
ftp.retrbinary('RETR README', open('README', 'wb').write)
Now I have a requirement that the file downloaded over FTP needs to have the same last modified time as the file on the FTP server itself. Assuming I could parse out the time from
ftp.retrlines('list'), how can I set the modified time on the downloaded file?
I’m on a unix based OS if that matters.
import os os.utime(path_to_file, (access_time, modification_time))
More elaborate example: https://www.tutorialspoint.com/python/os_utime.htm
There are 2 ways to do this. One is the
os.utime example which
is required if you are setting the timestamp on a file that has no
However, if you are copying the files with
shutil.copy() you have a
reference file. Then if you want the permission bits, last access time,
last modification time, and flags also copied, you can use
shutil.copystat() immediately after the
And then there is
shutil.copy2 which is intended to do both at once…