Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if the file is newer then some other file? [duplicate]

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.

Modification

  • os.stat(FILE).st_mtime

  • os.path.getmtime(FILE)

Creation

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.

like image 996
Adobe Avatar asked Oct 10 '12 10:10

Adobe


4 Answers

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.

like image 156
root Avatar answered Nov 12 '22 00:11

root


import os
f1 = os.path.getmtime('file1')
f2 = os.path.getmtime('file2')

if f1 > f2:

check for modified time might be one solution

like image 45
James Avatar answered Nov 12 '22 00:11

James


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
like image 31
Rohit Jain Avatar answered Nov 12 '22 00:11

Rohit Jain


import os

def comp(path1, path2):    
    return os.stat(path1).st_ctime > os.stat(path2).st_ctime
like image 1
defuz Avatar answered Nov 12 '22 00:11

defuz