- 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 set 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.
>>> 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.
>>> import os >>> print os.path.getmtime('my_file') 1505928275.3081832
- How to get 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 find and sort files based on modification date and time in linux
- Creation of .ASM file using a text editor
- 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 the creation date of a MySQL table?
- How to set default date time as system date time in MySQL?
- Creation of virtual environments using Python
- How to check file last access time using Python?
- Set current date and time to timestamp in MySQL
- How to get current date and time in Python?
- How to get formatted date and time in Python?