Problem
I have a script that needs to do something depending on the creation and modification dates of files, but it must run on both Linux and Windows.
What is the best approach to acquire file creation and update dates/times in Python that is cross-platform?
Asked by Mark Biek
Solution #1
It’s simple to get a cross-platform modification date by using the os.path function. getmtime(path) returns the Unix timestamp of the latest modification of the file at path.
Getting file creation dates, on the other hand, is time-consuming and platform-dependent, with differences even within the three major operating systems:
Putting this all together, cross-platform code should look something like this…
import os
import platform
def creation_date(path_to_file):
"""
Try to get the date that a file was created, falling back to when it was
last modified if that isn't possible.
See http://stackoverflow.com/a/39501288/1709587 for explanation.
"""
if platform.system() == 'Windows':
return os.path.getctime(path_to_file)
else:
stat = os.stat(path_to_file)
try:
return stat.st_birthtime
except AttributeError:
# We're probably on Linux. No easy way to get creation dates here,
# so we'll settle for when its content was last modified.
return stat.st_mtime
Answered by Mark Amery
Solution #2
There are a few options available to you. For example, the os.path.getmtime and os.path.getctime functions can be used:
import os.path, time
print("last modified: %s" % time.ctime(os.path.getmtime(file)))
print("created: %s" % time.ctime(os.path.getctime(file)))
Alternatively, you can 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))
On *nix systems, ctime() refers to the last time the inode contents changed, not the creation time. (Thanks to kojiro for clarifying this in the comments by offering a link to an intriguing blog piece.)
Answered by Bryan Oakley
Solution #3
os.path.getmtime is the best function to utilize for this (). Internally, os.stat(filename).st mtime is used.
You can get the modification date as a datetime object using the datetime module, which is the best for handling timestamps:
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)
Answered by Christian Oudard
Solution #4
The object-oriented pathlib module interface, which provides wrappers for much of the os module, is available in Python 3.4 and higher. Here’s an example of how to collect file statistics.
>>> import pathlib
>>> fname = pathlib.Path('test.py')
>>> assert fname.exists(), f'No such file: {fname}' # check that the file exists
>>> print(fname.stat())
os.stat_result(st_mode=33206, st_ino=5066549581564298, st_dev=573948050, st_nlink=1, st_uid=0, st_gid=0, st_size=413, st_atime=1523480272, st_mtime=1539787740, st_ctime=1523480272)
Refer to the documentation for further information on what os.stat result contains. You want fname.stat().st mtime for the modification time:
>>> import datetime
>>> mtime = datetime.datetime.fromtimestamp(fname.stat().st_mtime, tz=datetime.timezone.utc)
>>> print(mtime)
datetime.datetime(2018, 10, 17, 10, 49, 0, 249980)
On Windows, you’d use fname.stat().st ctime:fname.stat().st ctime:fname.stat().st ctime:fname.stat().st ctime:fname.stat().st ctime:fname.stat().st_
>>> ctime = datetime.datetime.fromtimestamp(fname.stat().st_ctime, tz=datetime.timezone.utc)
>>> print(ctime)
datetime.datetime(2018, 4, 11, 16, 57, 52, 151953)
More information and examples for the pathlib module may be found in this article.
Answered by Steven C. Howell
Solution #5
os.stat
You should definitely use os.path in newer programs. getmtime() is a function that returns the current time (thanks, Christian Oudard).
However, it should be noted that it returns a time t floating point value with fraction seconds (if your OS supports it).
Answered by Martin Beckett
Post is based on https://stackoverflow.com/questions/237079/how-to-get-file-creation-and-modification-date-times