Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (47.6k 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 (106k 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.

For more information, kindly refer to our Python course.

Related questions

0 votes
1 answer
asked Jun 26, 2019 in Python by Anurag (33.1k points)
+3 votes
2 answers
0 votes
2 answers
asked Aug 23, 2019 in Python by Sammy (47.6k points)
0 votes
1 answer
asked Oct 14, 2019 in Python by Sammy (47.6k points)
0 votes
1 answer
asked Sep 25, 2019 in Python by Sammy (47.6k points)

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...