- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
- Python Advanced Tutorial
- Python - Classes/Objects
- Python - Reg Expressions
- Python - CGI Programming
- Python - Database Access
- Python - Networking
- Python - Sending Email
- Python - Multithreading
- Python - XML Processing
- Python - GUI Programming
- Python - Further Extensions
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
- Related Articles
- 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 get current date and time in Python?
- How to get formatted date and time in Python?
- How to find and sort files based on modification date and time in linux
- How do you get a directory listing sorted by creation date in Python?
- 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?
- Creation of .ASM file using a text editor
- How to get the creation time of recently created table in MySQL?