在python中 - 如何检查 - 文件是否比其他文件更新?
修改:
有创建时间和修改时间。
问题应该明确说明所需的属性。
os.stat(FILE).st_mtime
os.path.getmtime(FILE)
os.path.getctime(FILE)
和os.stat(FILE).st_ctime
不会在类Unix操作系统上创建创建时间。 Link root提供了如何在类似Unix的盒子上找到创建时间的解决方案。
答案 0 :(得分:10)
import os
f1 = os.path.getmtime('file1')
f2 = os.path.getmtime('file2')
if f1 > f2:
检查修改时间可能是一个解决方案
答案 1 :(得分:8)
您也可以使用os.path.getctime
。如果在True
之前创建file1
,则file2
会返回False
。
import os.path
os.path.getctime('file1') < os.path.getctime('file2')
编辑:请注意,Unix中的there is no cross platform solution to your question - ctime()表示上次更改时间,而不是创建时间。 The same applies when using os.stat(file).st_ctime.
Here似乎可以在unix机器上运行。
答案 2 :(得分:3)
在任何文件上使用os.stat
,会为您提供一组有关您文件的10种不同统计信息。其中一项统计信息是creation time
- &gt; st_ctime
..您可以使用它来计算两个文件的创建时间之间的差异..
>>> import os
>>> os.stat("D:\demo.pl")
nt.stat_result(st_mode=33206, st_ino=0L, st_dev=0, st_nlink=0, st_uid=0,
st_gid=0, st_size=135L, st_atime=1348227875L, st_mtime=1348228036L,
st_ctime=1348227875L)
>>> os.stat("D:\demo.pl").st_ctime
1348227875.8448658
答案 3 :(得分:1)
import os
def comp(path1, path2):
return os.stat(path1).st_ctime > os.stat(path2).st_ctime