Possible Duplicate:
python: which file is newer & by how much time
In python -- how do I check -- if the file is newer then some other file?
Edit:
There are creation time and modification time.
The question should state the desired property explicitly.
os.stat(FILE).st_mtime
os.path.getmtime(FILE)
os.path.getctime(FILE)
and os.stat(FILE).st_ctime
doesn't give creation time on Unix-like OSes. Link by root has the solution on how to find out the creation time on Unix-like boxes.
You can also use os.path.getctime
. This example will return True
if file1
was created before file2
and False
otherwise.
import os.path
os.path.getctime('file1') < os.path.getctime('file2')
EDIT: Note that there is no cross platform solution to your question -- ctime() in Unix means last change time, not create time. The same applies when using os.stat(file).st_ctime.
Here seems to be something that could work on unix machines.
import os
f1 = os.path.getmtime('file1')
f2 = os.path.getmtime('file2')
if f1 > f2:
check for modified time might be one solution
Using os.stat
on any file, gives you a set of 10 different stats about your file.. One of the stat is creation time
-> st_ctime
.. You can use that to calculate the difference between your creation time of two files..
>>> 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
import os
def comp(path1, path2):
return os.stat(path1).st_ctime > os.stat(path2).st_ctime
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With