Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Use the <a href="https://docs.python.org/3/library/os.html#os.stat_result.st_birthtime" rel="nofollow noreferrer"><strong><code>st_birthtime</code></strong></a> property on the result of a call to <a href="https://docs.python.org/3/library/os.html#os.stat" rel="nofollow noreferrer"><code>os.stat()</code></a> (or <code>fstat</code>/<code>lstat</code>).</p> <pre><code>def get_creation_time(path): return os.stat(path).st_birthtime </code></pre> <p>You can convert the integer result to a datetime object using <a href="https://docs.python.org/3/library/datetime.html#datetime.datetime.fromtimestamp" rel="nofollow noreferrer"><code>datetime.datetime.fromtimestamp()</code></a>.</p> <p><sub>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.</sub></p> <hr> <p>Using <code>ctypes</code> to access the system call <code>stat64</code> (works with Python 2.5+):</p> <pre><code>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 </code></pre> <p>Using <code>subprocess</code> to call the <code>stat</code> utility:</p> <pre><code>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()) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload