Change language

How can I find out the date and time when a file was created and modified in Python?

|

To get the creation time of a file, you can use os.path.getctime (file_path) on Windows. On UNIX systems, you cannot use the same function that returns the last time a file’s attributes or content was changed. To get the creation time on UNIX-based systems, use the st_birthtime attribute of the stat tuple.

Example

On Windows &

""" import os""" print os.path.getctime (’my_file’) 1505928271.0689342 

This gives the time in seconds since the beginning of the epoch. For UNIX systems,





 import os stat = os.stat (path_to_file) try: print (stat.st_birthtime) except AttributeError: # Probably on Linux. No easy way to get creation dates here, # so we’ll settle for when its content was last modified. print (stat.st_mtime) 

Output

This will give output &

 1505928271.0689342 

You can use os.path.getmtime (path) to get the modification time of a file. Cross-platform supported.

""" import os""" print os.path.getmtime (’my_file’) 1505928275.3081832 

 

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

News


Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method