- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to get creation and modification date/time of a file using Python?
To get the creation time of a file, you can use the os.path.getctime(file_path) on windows. On UNIX systems, you cant use the same function as it returns the last time that the file's attributes or content were changed. To get the creation time on UNIX based systems, use the st_birthtime attribute of the stat tuple.
On Windows −
>>> import os >>> print os.path.getctime('my_file') 1505928271.0689342
It gives the time in the number of seconds since 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)
This will give the output −
For getting the modification time for a file, you can use the os.path.getmtime(path). It is supported cross-platform.For example,
>>> import os >>> print os.path.getmtime('my_file') 1505928275.3081832
- How to set creation and modification date/time of a file using Python?
- How to get file creation & modification date/times in Python?
- Get the creation time of a file in C#
- How to get the creation date of a MySQL table?
- How to find and sort files based on modification date and time in linux
- How to get current date and time in Python?
- How to get formatted date and time in Python?
- Creation of .ASM file using a text editor
- How do I get the creation date of a MySQL table?
- How to check the Date and time of Access (last modified) of a File using Java?
- How to print current date and time using Python?
- How to get stat of a file using Python?
- How do you get a directory listing sorted by creation date in Python?
- How to get the creation time of recently created table in MySQL?