w3resource

Python: Get file creation and modification date/times

Python Basic: Exercise-64 with Solution

Write a Python program that retrieves the date and time of file creation and modification.

Sample Solution:

Python Code :

# Import the 'os.path' and 'time' modules to work with file system paths and timestamps.
import os.path, time

# Print the last modified timestamp of the file "test.txt" in a human-readable format.
print("Last modified: %s" % time.ctime(os.path.getmtime("test.txt")))

# Print the creation timestamp of the file "test.txt" in a human-readable format.
print("Created: %s" % time.ctime(os.path.getctime("test.txt")))

Sample Output:

Last modified: Wed Apr 19 11:36:23 2017                                                                       
Created: Wed Apr 19 11:36:23 2017 

Flowchart:

Flowchart: Get file creation and modification date/times.

Python Code Editor:

 

Previous: Write a Python program to get an absolute file path.
Next: Write a Python program to convert seconds to day, hour, minutes and seconds.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.