w3resource

Python: Get file creation and modification date/times


File Timestamps

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.

For more Practice: Solve these Related Problems:

  • Write a Python program to find the last access time of a given file.
  • Write a Python program to get the timestamp of a file and convert it into a human-readable format.
  • Write a Python program to compare the creation times of two different files.
  • Write a Python program to check if a file has been modified in the last 24 hours.

Go to:


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.

Python Code Editor:

 

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.