[ACCEPTED]-Get file creation time with Python on Mac-macos
st_birthtime property on the result of a call 4 to
def get_creation_time(path): return os.stat(path).st_birthtime
You can convert the integer result 3 to a datetime object using
For some reason I don't think this worked on Mac OS X when this answer was first written, but I could be mistaken, and it does work now, even with older versions of Python. The old answer is below for posterity.
ctypes to access 2 the system call
stat64 (works with Python 2.5+):
from ctypes import * class struct_timespec(Structure): _fields_ = [('tv_sec', c_long), ('tv_nsec', c_long)] class struct_stat64(Structure): _fields_ = [ ('st_dev', c_int32), ('st_mode', c_uint16), ('st_nlink', c_uint16), ('st_ino', c_uint64), ('st_uid', c_uint32), ('st_gid', c_uint32), ('st_rdev', c_int32), ('st_atimespec', struct_timespec), ('st_mtimespec', struct_timespec), ('st_ctimespec', struct_timespec), ('st_birthtimespec', struct_timespec), ('dont_care', c_uint64 * 8) ] libc = CDLL('libc.dylib') # or /usr/lib/libc.dylib stat64 = libc.stat64 stat64.argtypes = [c_char_p, POINTER(struct_stat64)] def get_creation_time(path): buf = struct_stat64() rv = stat64(path, pointer(buf)) if rv != 0: raise OSError("Couldn't stat file %r" % path) return buf.st_birthtimespec.tv_sec
subprocess to call the
import subprocess def get_creation_time(path): p = subprocess.Popen(['stat', '-f%B', path], stdout=subprocess.PIPE, stderr=subprocess.PIPE) if p.wait(): raise OSError(p.stderr.read().rstrip()) else: return int(p.stdout.read())
ctime differs on the platform: On some systems (like Unix) is the time of the last metadata change, and, on others (like Windows), is the creation time. That's 15 because Unices usually don't preserve the 14 "original" creation time.
That 13 said you can access all information that 12 the OS provides with the stat module.
The stat 11 module defines constants and functions for 10 interpreting the results of os.stat(), os.fstat() and 9 os.lstat() (if they exist). For complete 8 details about the stat, fstat and lstat 7 calls, consult the documentation for your 6 system.
The “ctime” as reported 5 by the operating system. On some systems 4 (like Unix) is the time of the last metadata 3 change, and, on others (like Windows), is 2 the creation time (see platform documentation 1 for details).
By lack of a good utility, I've created 5 crtime.
pip install crtime
Then you can use it like:
sudo crtime ./
1552938281 /home/pascal/crtime/.gitignore 1552938281 /home/pascal/crtime/README.md 1552938281 /home/pascal/crtime/crtime 1552938281 /home/pascal/crtime/deploy.py 1552938281 /home/pascal/crtime/setup.cfg 1552938281 /home/pascal/crtime/setup.py 1552938961 /home/pascal/crtime/crtime.egg-info 1552939447 /home/pascal/crtime/.git 1552939540 /home/pascal/crtime/build 1552939540 /home/pascal/crtime/dist
Note 4 that for large directories it will be easily 3 1000x faster than
xstat that is sometimes mentioned, as 2 this creates a temporary file and then executes 1
stat calls for all files at once.
More Related questions