在Linux和Windows上获得文件创建和修改日期/时间的最佳跨平台方法是什么?
当前回答
有两个方法可以获得mod时间,os.path.getmtime()或os.stat(),但ctime跨平台不可靠(见下文)。
os.path.getmtime()
getmtime(路径) 返回最后一次修改路径的时间。返回值是一个给出 自epoch开始的秒数(请参阅time模块)。提高操作系统。如果文件存在,则错误 不存在或无法访问。1.5.2新版功能。在2.3版更改:如果 os.stat_float_times()返回True,结果是一个浮点数。
os.stat()
stat(path) Perform a stat() system call on the given path. The return value is an object whose attributes correspond to the members of the stat structure, namely: st_mode (protection bits), st_ino (inode number), st_dev (device), st_nlink (number of hard links), st_uid (user ID of owner), st_gid (group ID of owner), st_size (size of file, in bytes), st_atime (time of most recent access), st_mtime (time of most recent content modification), st_ctime (platform dependent; time of most recent metadata change on Unix, or the time of creation on Windows):
>>> import os
>>> statinfo = os.stat('somefile.txt')
>>> statinfo
(33188, 422511L, 769L, 1, 1032, 100, 926L, 1105022698,1105022732, 1105022732)
>>> statinfo.st_size
926L
>>>
在上面的例子中,您将使用statinfo。St_mtime或statinfo。St_ctime分别获取mtime和ctime。
其他回答
以跨平台的方式获取某种修改日期很容易——只需调用os.path.getmtime(path),就会得到文件在path上最后一次修改的Unix时间戳。
另一方面,获取文件创建日期非常繁琐,而且依赖于平台,甚至在三大操作系统之间也有所不同:
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 through os.path.getctime() or the .st_ctime attribute of the result of a call to os.stat(). This won't work on Unix, where the ctime 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 to os.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 in st_crtime) , the Linux kernel offers no way of accessing them; in particular, the structs it returns from stat() calls in C, as of the latest kernel version, don't contain any creation date fields. You can also see that the identifier st_crtime doesn't currently feature anywhere in the Python source. At least if you're on ext4, the data is attached to the inodes in the file system, but there's no convenient way of accessing it. 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.
把这些放在一起,跨平台代码应该是这样的……
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
有两个方法可以获得mod时间,os.path.getmtime()或os.stat(),但ctime跨平台不可靠(见下文)。
os.path.getmtime()
getmtime(路径) 返回最后一次修改路径的时间。返回值是一个给出 自epoch开始的秒数(请参阅time模块)。提高操作系统。如果文件存在,则错误 不存在或无法访问。1.5.2新版功能。在2.3版更改:如果 os.stat_float_times()返回True,结果是一个浮点数。
os.stat()
stat(path) Perform a stat() system call on the given path. The return value is an object whose attributes correspond to the members of the stat structure, namely: st_mode (protection bits), st_ino (inode number), st_dev (device), st_nlink (number of hard links), st_uid (user ID of owner), st_gid (group ID of owner), st_size (size of file, in bytes), st_atime (time of most recent access), st_mtime (time of most recent content modification), st_ctime (platform dependent; time of most recent metadata change on Unix, or the time of creation on Windows):
>>> import os
>>> statinfo = os.stat('somefile.txt')
>>> statinfo
(33188, 422511L, 769L, 1, 1032, 100, 926L, 1105022698,1105022732, 1105022732)
>>> statinfo.st_size
926L
>>>
在上面的例子中,您将使用statinfo。St_mtime或statinfo。St_ctime分别获取mtime和ctime。
在Python 3.4及以上版本中,您可以使用面向对象的pathlib模块接口,其中包括大部分os模块的包装器。下面是一个获取文件统计信息的示例。
>>> import pathlib
>>> fname = pathlib.Path('test.py')
>>> assert fname.exists(), f'No such file: {fname}' # check that the file exists
>>> print(fname.stat())
os.stat_result(st_mode=33206, st_ino=5066549581564298, st_dev=573948050, st_nlink=1, st_uid=0, st_gid=0, st_size=413, st_atime=1523480272, st_mtime=1539787740, st_ctime=1523480272)
有关操作系统的更多信息。Stat_result包含,请参考文档。对于修改时间,你需要fname.stat().st_mtime:
>>> import datetime
>>> mtime = datetime.datetime.fromtimestamp(fname.stat().st_mtime, tz=datetime.timezone.utc)
>>> print(mtime)
datetime.datetime(2018, 10, 17, 10, 49, 0, 249980)
如果你想要Windows上的创建时间,或者Unix上最新的元数据更改,你可以使用fname.stat().st_ctime:
>>> ctime = datetime.datetime.fromtimestamp(fname.stat().st_ctime, tz=datetime.timezone.utc)
>>> print(ctime)
datetime.datetime(2018, 4, 11, 16, 57, 52, 151953)
本文为pathlib模块提供了更多有用的信息和示例。
os.stat
在更新的代码中,您可能应该使用os.path.getmtime()(谢谢,Christian Oudard)。
但是请注意,它返回一个带分数秒的浮点值time_t(如果您的操作系统支持它)。
你有几个选择。首先,你可以使用os.path.getmtime和os.path.getctime函数:
import os.path, time
print("last modified: %s" % time.ctime(os.path.getmtime(file)))
print("created: %s" % time.ctime(os.path.getctime(file)))
你的其他选择是使用os.stat:
import os, time
(mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime) = os.stat(file)
print("last modified: %s" % time.ctime(mtime))
注意:ctime()不是指*nix系统上的创建时间,而是指inode数据最后一次更改的时间。(感谢kojiro在评论中提供了一个有趣的博客文章链接,使这一事实更加明确。)
推荐文章
- 有没有办法在python中做HTTP PUT
- “foo Is None”和“foo == None”之间有什么区别吗?
- 类没有对象成员
- Django模型“没有显式声明app_label”
- 熊猫能自动从CSV文件中读取日期吗?
- 在python中zip的逆函数是什么?
- 有效的方法应用多个过滤器的熊猫数据框架或系列
- 如何检索插入id后插入行在SQLite使用Python?
- 我如何在Django中添加一个CharField占位符?
- 如何在Python中获取当前执行文件的路径?
- 我如何得到“id”后插入到MySQL数据库与Python?
- super()失败,错误:TypeError "参数1必须是类型,而不是classobj"当父不继承对象
- Python内存泄漏
- 实现嵌套字典的最佳方法是什么?
- 如何在tensorflow中获得当前可用的gpu ?