In this tutorial, you’ll learn how to get the creation and modification datetime of a file in Python. This is useful for cleaning up old log files, finding the newest file in a folder, or showing a “last updated” date.
Python stores these times as timestamps (seconds since 1 January 1970). We’ll get them with the os and pathlib modules, then convert them into readable dates.
os.path module:
os.path.getmtime(path): The time the file’s content was last modified. Works on every operating system.os.path.getatime(path): The time the file was last accessed (read).os.path.getctime(path): On Windows, the creation time (deprecated for this purpose since Python 3.12). On Linux and macOS, the time the file’s metadata last changed, which is not the creation time.os.stat(path).st_birthtime: The real creation time, where the system provides it (macOS, BSD, and Windows on Python 3.12+).
pathlib module:
Path(path).stat().st_mtime: The modern, object-oriented way to get the modification time.Path(path).stat().st_birthtime/st_ctime: The same creation-time rules as above.
Table of contents
How to Get File Modification and Creation Time in Python
The steps below show how to use the os.path module and the datetime module to get the modification and creation time of a file in Python.
- Import the os and datetime modules
The
os.pathmodule has functions to read a file’s metadata. The datetime module converts the timestamps it returns into readable dates. - Get the modification time with getmtime()
os.path.getmtime(path)returns the time of the last modification as a float timestamp. The path can be absolute or relative to the current folder. - Convert the timestamp to a datetime
Pass the timestamp to
datetime.fromtimestamp()to get a datetime object in your local time. Then format it withstrftime(), for example asDD-MM-YYYY HH:MM:SS. - Get the creation time
On Windows,
os.path.getctime(path)returns the creation time. On macOS (and on Windows with Python 3.12+), useos.stat(path).st_birthtime. On Linux,getctime()returns the last metadata change instead, and the creation time usually isn’t available to Python.
Example to Get File Modification and Creation Time
To make this example runnable anywhere, we first create a small file. In your own code, use the path to an existing file, for example r"E:\demos\sample.txt" on Windows or "/Users/me/sample.txt" on macOS.
Output:
Timestamp: 1796026075.6479409 Modified on: 2026-11-30 13:37:55.647941 Formatted: 30-11-2026 13:37:55 ctime: 2026-11-30 13:37:55.647941
Your output will show the date and time when you run the program, because we just created the file. To display dates in other formats, see Python Datetime formatting.
Get File Creation Datetime on Mac and Unix Systems
- On macOS and BSD systems, the
st_birthtimeattribute of the result ofos.stat()holds the file’s creation time. - On Windows,
st_birthtimeis available from Python 3.12. Before that, usest_ctime(orgetctime()). - On Linux,
os.stat()doesn’t providest_birthtime, so accessing it raises anAttributeError.
This function works on every system. It returns the creation time where it’s available and falls back to the modification time otherwise:
Output:
Created (or modified) on: 2026-11-30 13:37:55.663400
Why is the creation time so hard to get? Not every file system records it, and operating systems expose it in different ways. That’s why the modification time (st_mtime) is the most reliable, portable value. If your program decides things based on file age, use the modification time.
pathlib Module to Get the Creation and Modification Datetime of a File
The pathlib module (Python 3.4+) represents file paths as objects. It works the same way on Windows, macOS, and Linux, and many developers find it cleaner than os.path.
- Create a Path object:
Path('sample.txt')represents the file. On Windows it becomes aWindowsPath, and on other systems aPosixPath. - Call the stat() method: It returns the file’s metadata, including its size and timestamps.
- Read the timestamp attributes:
st_mtimeis the last modification time andst_atimethe last access time.st_ctimefollows the same platform rules asgetctime(). These are attributes, so write them without parentheses. - Convert to a datetime: Pass the timestamp to
datetime.fromtimestamp().
Example
Output:
Size: 14 bytes Modified on: 2026-11-30 13:37:55 Accessed on: 2026-11-30 13:37:55
Note: Don’t rely on the access time (st_atime). To improve performance, many systems update it only occasionally or not at all, so it may not change when a file is read.
All of these functions raise a FileNotFoundError if the file doesn’t exist. Check the path with Path.exists() first, or wrap the call in a try-except block.
Get the Modification Time in UTC
datetime.fromtimestamp(ts) returns your local time. If you share the time with other systems or store it in a database, convert it to UTC by passing tz=timezone.utc. Here we use os.utime() to set a known modification time so the output is the same on every computer:
Output:
UTC: 2022-01-15 10:30:00+00:00 ISO: 2022-01-15T10:30:00+00:00
os.utime(path, (atime, mtime)) changes a file’s access and modification times. Calling os.utime(path) without times sets both to now, much like the Unix touch command (or Path.touch() in pathlib).
Find Files Modified in the Last N Days
A common real-world task is finding recently changed files, or old files to delete. Compare each file’s modification time with a cutoff date:
Output:
last_week.log recent old.log older than 7 days today.log recent
To delete old files, replace the print() with file.unlink() for the old ones. Test with print() first, because deleted files don’t go to the recycle bin.
Sort Files by Modification Time
Use sorted() with a key that returns each file’s st_mtime. Add reverse=True to put the newest file first:
Output:
Newest first: ['mar.csv', 'feb.csv', 'jan.csv'] Latest file: mar.csv
To learn more about working with files, see the Python file handling tutorials.
Summary
| Task | Use |
|---|---|
| Last modification time | os.path.getmtime(p) or Path(p).stat().st_mtime |
| Last access time | os.path.getatime(p) or Path(p).stat().st_atime |
| Creation time (macOS, Windows 3.12+) | os.stat(p).st_birthtime |
| Creation time (Windows, Python 3.11 and older) | os.path.getctime(p) |
| Timestamp to local datetime | datetime.fromtimestamp(ts) |
| Timestamp to UTC datetime | datetime.fromtimestamp(ts, tz=timezone.utc) |
| Change modification time | os.utime(p, (atime, mtime)) |

it is so sad that everyone is still using old functions to set datetime? newer function allows to set any date (even below 1970), But I cannot find the systax of it in Python.
A very good article: an overview at the beginning, detailed description and examples in the main text, just in the style of K&R’s “C Programming Language”, I like it!
How can I get file creation datetime from SFTP location?