St_birthtime not available

Hello!
I’m running python3.12 on Ubuntu 24.04 with kernel 6.14.0-33-generic on an ext4 filesystem. The code

import os
...
os.stat(myfile).st_birthtime

generates:

AttributeError: ‘os.stat_result’ object has no attribute ‘st_birthtime’

and the same with st_birthtime_ns. The documentation warns that (in both cases)

This attribute is not always available, and may raise AttributeError.

But can this be fixed some way? For example, is it possible to install some packages to provide such an attribute? Or are there some conditions (some kernel version or some distribution) under which this attribute can be provided?

This is actively being looked at :slight_smile:. Currently st_birthtime is only available on Windows, FreeBSD, and macOS. For windows the stat abstraction is a translation from its system APIs, and for FreeBSD + macOS their stat system call returns st_birthtime and CPython makes it available.

For Linux though the statx system call has to be used (statx(2) - Linux manual page). Exposing that in CPython is currently being worked on as part of Use `statx(2)` system call on Linux for extended `os.stat` information · Issue #83714 · python/cpython · GitHub. That work, if it lands, should make a way to get st_birthtime for Python 3.15. Older Python versions, such as 3.12 however would need to implement in a third party library / new features aren’t typically backported to older Python versions.

3 Likes