How do I get file creation and modification date/times?
Here's a quick recipe to fetch the creation and modification dates of a file in Python. We'll use os.path.getmtime()
for the last modified date and pathlib.Path().stat().st_ctime
for the creation date:
This will give you readable modification and creation dates directly. Straightforward and neat! Just note that the creation time is handled differently depending on the operating system.
Digging into file timestamps
Files keep track of their historical metadata, somewhat like a person's diary. So, when you sort documents or verify compliance, knowing a file's creation and modification dates becomes imperative.
Handling differences across platforms
Windows and Unix-ish systems (including Linux and macOS) have different ways of time-keeping birthing and aging of files. Consider these nuances when fetching timestamps:
- Windows:
os.path.getctime()
got your back for file creation time. - macOS/Unix:
os.stat().st_birthtime
will get you the birth date of a file. - Linux: Notoriously, it doesn't keep track of file birth times - we know, right? But you can use
os.stat().st_mtime
as a close substitute.
For cross-platform code, you'll need to use platform.system()
to find out the OS and then decide which method to use. Clever, huh?
Making timestamps human-friendly
Epoch timestamps aren't exactly easy on human eyes, but we can tame them using datetime.fromtimestamp()
or time.ctime()
:
Want to make your timestamp look stylish? Python's strftime
method can help you customize the format:
Taking a modern, object-oriented approach with pathlib
Introduced in Python 3.4, pathlib
provides a more object-oriented approach to file operations. It's more powerful and certainly more Pythonic. Here's a simple example:
The idiosyncrasies of timestamps
Even in the world of file timestamps, not all things are created equal. Here are some edge cases to look out for:
- Linux's inode change time:
os.path.getctime()
on Linux is somewhat misleading as it returns the last inode change time, not the file creation time. - Filesystems Implications: Filesystems like
ext4
on Linux do store file creation time, but Python'sos
module hasn't gotten the memo yet. - Timezone Considerations: Python timestamps are in UTC. If you need to display in local time, make sure to convert:
Troubleshooting and further research
File System Support: Some file systems don't support creation time, without which you're out of luck. Permissions: You can't access file metadata without corresponding permissions. Symbolic Links: Make sure you fetch the metadata of the actual file, not the symbolic link.
For coders going the extra mile
Bulk Operations: If you deal with numerous files, use batch operations to improve efficiency.
Caching: Frequently accessed files? Consider caching their timestamps to avoid redundant disk I/O.
Alternative Libraries: Try scandir
for efficient directory traversal or Watchdog to monitor file system events in real-time.
Was this article helpful?