问题描述
限时送ChatGPT账号..我有一个 python 脚本,它使用 ftplib 通过 FTP 下载文件.
I have a python script that downloads a file over FTP using ftplib.
我当前的下载代码看起来就像 ftp lib 文档中的示例:
My current download code looks just like the example in the ftp lib docs:
ftp.retrbinary('RETR README', open('README', 'wb').write)
现在我有一个要求,即通过 FTP 下载的文件需要与 FTP 服务器本身上的文件具有相同的最后修改时间.假设我可以从 ftp.retrlines('list')
中解析出时间,我如何在下载的文件上设置修改时间?
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?
如果重要的话,我使用的是基于 Unix 的操作系统.
I'm on a unix based OS if that matters.
推荐答案
使用 os.utime
:
import os
os.utime(path_to_file, (access_time, modification_time))
更详细的例子:https://www.tutorialspoint/python/os_utime.htm
这篇关于如何从python设置文件的最后修改时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论