如何从python设置文件的最后修改时间?

编程入门 行业动态 更新时间:2024-10-27 21:19:38
本文介绍了如何从python设置文件的最后修改时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送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:关键词]

本文发布于:2023-04-28 17:08:26,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1176877.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件   时间   python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!