如何从python设置文件的最后修改时间? [英] How can I set the last modified time of a file from python?

查看:50
本文介绍了如何从python设置文件的最后修改时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 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.com/python/os_utime.htm

这篇关于如何从python设置文件的最后修改时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆