I have a script that needs to do some stuff based on file creation & modification dates but has to run on Linux & Windows.
What's the best cross-platform way to get file creation & modification date/times in Python?
I have a script that needs to do some stuff based on file creation & modification dates but has to run on Linux & Windows.
What's the best cross-platform way to get file creation & modification date/times in Python?
You have a couple of choices. For one, you can use the os.path.getmtime and os.path.getctime functions:
import os.path, time
print "last modified: %s" % time.ctime(os.path.getmtime(file))
print "created: %s" % time.ctime(os.path.getctime(file))
Your other option is to use 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)
There are two methods to get the mod time, os.path.getmtime() or os.stat(), but the ctime is not reliable cross-platform (see below).
getmtime(path)
*Return the time of last modification of path. The return value is a number giving the
number of seconds since the epoch (see the time module). Raise os.error if the file does
not exist or is inaccessible. New in version 1.5.2. Changed in version 2.3: If
os.stat_float_times() returns True, the result is a floating point number.*
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
>>>
In the above example you would use statinfo.st_mtime or statinfo.st_ctime to get the mtime and ctime, respectively.
os.stat
returns a tagged-tuple with st_mtime
and st_ctime
attributes. The modification time is st_mtime
on both platforms; unfortunately, on Windows, ctime
means "creation time", whereas on POSIX it means "change time". I'm not aware of any way to get the creation time on POSIX platforms.
>>> import os
>>> os.stat('feedparser.py').st_mtime
1136961142.0
>>> os.stat('feedparser.py').st_ctime
1222664012.233
>>>
The best function to use for this is os.path.getmtime(). Internally, this just uses os.stat(filename).st_mtime
.
The datetime module is the best manipulating timestamps, so you can get the modification date as a datetime
object like this:
import os
import datetime
def modification_date(filename):
t = os.path.getmtime(filename)
return datetime.datetime.fromtimestamp(t)
Usage example:
>>> d = modification_date('/var/log/syslog')
>>> print d
2009-10-06 10:50:01
>>> print repr(d)
datetime.datetime(2009, 10, 6, 10, 50, 1)