0 votes
1 view
in Python by (47.8k points)

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?

1 Answer

0 votes
by (107k points)

To get the file modification date/time we have many methods some of them are as follows:-

The first method 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)))

Another important method 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))

The important point to note here is the ctime() does not refer to creation time on the system but rather the last time the inode data changed.

Related questions

0 votes
1 answer
asked Aug 27, 2019 in Python by Sammy (47.8k points)
0 votes
1 answer
asked Jun 26, 2019 in Python by Anurag (33.2k points)
+3 votes
2 answers
0 votes
2 answers
asked Aug 23, 2019 in Python by Sammy (47.8k points)
+1 vote
2 answers
asked May 30, 2019 in Python by Ritik (3.5k points)
Welcome to Intellipaat Community. Get your technical queries answered by top developers !