问题描述
我有一个脚本,需要根据文件创建和添加内容来做一些事情.修改日期,但必须在 Linux 上运行& Windows .
I have a script that needs to do some stuff based on file creation & modification dates but has to run on Linux & Windows.
什么是最好的跨平台方式来创建文件和在 Python 中修改date/times
?
What's the best cross-platform way to get file creation & modification date/times
in Python?
推荐答案
以跨平台的方式获取某种修改日期很容易-只需调用 os.path.getmtime(path)
,您将获得Unix时间戳,该时间戳是最后一次修改path
处的文件的时间.
Getting some sort of modification date in a cross-platform way is easy - just call os.path.getmtime(path)
and you'll get the Unix timestamp of when the file at path
was last modified.
获取文件创建的日期是不固定的且依赖于平台,甚至在三大操作系统之间也有所不同:
Getting file creation dates, on the other hand, is fiddly and platform-dependent, differing even between the three big OSes:
- 在 Windows 上,文件的
ctime
(记录在 https://msdn.microsoft.com/zh-cn/library/14h5k7ff.aspx )存储其创建日期.您可以通过os.path.getctime()
在Python中访问此文件,或者通话结果的.st_ctime
属性到os.stat()
.该不会在Unix上起作用,其中ctime
是文件属性或的最后一次更改. - 在 Mac 上以及其他一些基于Unix的操作系统上,您可以使用
.st_birthtime
调用os.stat()
的结果的属性. -
在 Linux 上,目前是不可能的,至少没有为Python编写C扩展即可.尽管某些Linux常用的文件系统会创建存储创建日期(例如
ext4
将它们存储在st_crtime
中),Linux内核;特别是从最新的内核版本不包含任何创建日期字段.您还可以看到,标识符st_crtime
当前在 Python来源.至少如果您使用的是ext4
,数据 会附加到文件系统的inode中,但是没有方便的访问方法.
- On Windows, a file's
ctime
(documented at https://msdn.microsoft.com/en-us/library/14h5k7ff.aspx) stores its creation date. You can access this in Python throughos.path.getctime()
or the.st_ctime
attribute of the result of a call toos.stat()
. This won't work on Unix, where thectime
is the last time that the file's attributes or content were changed. - On Mac, as well as some other Unix-based OSes, you can use the
.st_birthtime
attribute of the result of a call toos.stat()
. On Linux, this is currently impossible, at least without writing a C extension for Python. Although some file systems commonly used with Linux do store creation dates (for example,
ext4
stores them inst_crtime
) , the Linux kernel offers no way of accessing them; in particular, the structs it returns fromstat()
calls in C, as of the latest kernel version, don't contain any creation date fields. You can also see that the identifierst_crtime
doesn't currently feature anywhere in the Python source. At least if you're onext4
, the data is attached to the inodes in the file system, but there's no convenient way of accessing it.
在Linux上,第二好的事情是通过mtime. rel ="noreferrer"> os.path.getmtime()
或 os.stat()
结果的.st_mtime
属性.这将为您提供最后一次修改文件内容的时间,这对于某些用例可能就足够了.
The next-best thing on Linux is to access the file's mtime
, through either os.path.getmtime()
or the .st_mtime
attribute of an os.stat()
result. This will give you the last time the file's content was modified, which may be adequate for some use cases.
将所有这些放在一起,跨平台代码应该看起来像这样……
Putting this all together, cross-platform code should look something like this...
import os
import platform
def creation_date(path_to_file):
"""
Try to get the date that a file was created, falling back to when it was
last modified if that isn't possible.
See http://stackoverflow.com/a/39501288/1709587 for explanation.
"""
if platform.system() == 'Windows':
return os.path.getctime(path_to_file)
else:
stat = os.stat(path_to_file)
try:
return stat.st_birthtime
except AttributeError:
# We're probably on Linux. No easy way to get creation dates here,
# so we'll settle for when its content was last modified.
return stat.st_mtime
这篇关于如何创建文件和在Python中修改日期/时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!