如何从python设置文件的上次修改时间?

时间:2023-02-13 15:06:01

I have a python script that downloads a file over FTP using ftplib.

我有一个python脚本,使用ftplib通过FTP下载文件。

My current download code looks just like the example in the ftp lib docs:

我当前的下载代码看起来就像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?

现在我要求通过FTP下载的文件需要与FTP服务器本身上的文件具有相同的最后修改时间。假设我可以从ftp.retrlines('list')解析时间,如何在下载的文件上设置修改时间?

I'm on a unix based OS if that matters.

如果重要的话,我正在使用基于unix的操作系统。

3 个解决方案

#1


46  

If you want to do this directly from python, you're looking for os.utime. The docs can give you more info.

如果你想直接从python这样做,你正在寻找os.utime。文档可以为您提供更多信息。

#2


24  

Use os.utime:

使用os.utime:

import os

os.utime(path_to_file, (access_time, modification_time))

#3


5  

There are 2 ways to do this. One is the os.utime example above which is required if you are setting the timestamp on a file that has no reference stats. 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 "shutil.copy()".

有两种方法可以做到这一点。一个是上面的os.utime示例,如果要在没有引用统计信息的文件上设置时间戳,则需要该示例。但是,如果您使用“shutil.copy()”复制文件,则会有一个参考文件。然后,如果您希望权限位,上次访问时间,上次修改时间和标志也被复制,您可以在“shutil.copy()”之后立即使用“shutil.copystat()”。

I have no idea why they don't add flags to "shutil.copy()" that does this in one single command. Seems like it was implemented by different authors. One implemented the copy and one implemented the copystat to fill in the missing feature in the former.

我不知道他们为什么不在“shutil.copy()”中添加标志,这些标志在一个命令中执行此操作。似乎它是由不同的作者实现的。一个实现了副本,一个实现了copystat以填充前者中缺少的功能。

And then there is "shutil.copy2" which is intended to do both at once...

然后有“shutil.copy2”,它打算同时做两件事......

#1


46  

If you want to do this directly from python, you're looking for os.utime. The docs can give you more info.

如果你想直接从python这样做,你正在寻找os.utime。文档可以为您提供更多信息。

#2


24  

Use os.utime:

使用os.utime:

import os

os.utime(path_to_file, (access_time, modification_time))

#3


5  

There are 2 ways to do this. One is the os.utime example above which is required if you are setting the timestamp on a file that has no reference stats. 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 "shutil.copy()".

有两种方法可以做到这一点。一个是上面的os.utime示例,如果要在没有引用统计信息的文件上设置时间戳,则需要该示例。但是,如果您使用“shutil.copy()”复制文件,则会有一个参考文件。然后,如果您希望权限位,上次访问时间,上次修改时间和标志也被复制,您可以在“shutil.copy()”之后立即使用“shutil.copystat()”。

I have no idea why they don't add flags to "shutil.copy()" that does this in one single command. Seems like it was implemented by different authors. One implemented the copy and one implemented the copystat to fill in the missing feature in the former.

我不知道他们为什么不在“shutil.copy()”中添加标志,这些标志在一个命令中执行此操作。似乎它是由不同的作者实现的。一个实现了副本,一个实现了copystat以填充前者中缺少的功能。

And then there is "shutil.copy2" which is intended to do both at once...

然后有“shutil.copy2”,它打算同时做两件事......