Checking a file's time stamp.

Larry Bates larry.bates at websafe.com`
Tue Aug 12 14:08:56 EDT 2008


Christian Heimes wrote:
> William Purcell wrote:
>> Hi all,
>> I am wanting to check to see the last time a file was edited. For 
>> example, I
>> have a directory containing two text files, file1.txt and file2.txt.   I
>> want to be able to process these files but only if they have been edited
>> since the last time they were processed. I think that I want to be 
>> able to
>> check the time stamp of each file. Can anyone tell me how to do that or
>> point me in a better direction of checking the last time a file was 
>> edited?
> 
> 
>  >>> import os
>  >>> stat = os.stat("/etc/passwd")
>  >>> print stat
> (33188, 362259, 2053L, 1, 0, 0, 1690, 1218550501, 1218118498, 1218118498)
>  >>> dir(stat)
> ['__add__', '__class__', '__contains__', '__delattr__', '__doc__', 
> '__eq__', '__ge__', '__getattribute__', '__getitem__', '__getslice__', 
> '__gt__', '__hash__', '__init__', '__le__', '__len__', '__lt__', 
> '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', 
> '__repr__', '__rmul__', '__setattr__', '__str__', 'n_fields', 
> 'n_sequence_fields', 'n_unnamed_fields', 'st_atime', 'st_blksize', 
> 'st_blocks', 'st_ctime', 'st_dev', 'st_gid', 'st_ino', 'st_mode', 
> 'st_mtime', 'st_nlink', 'st_rdev', 'st_size', 'st_uid']
>  >>> stat.st_mtime
> 1218118498.0
> 

use these shortcuts, IMHO they are easier than os.stat.

os.path.getmtime() - get modified time
os.path.atime()    - get last accessed time (careful some admins turn this
                      off on their servers for performance reasons)
os.path.ctime()    - get creation time


-Larry



More information about the Python-list mailing list