添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
玩足球的可乐  ·  Installing different ...·  3 小时前    · 
彷徨的青蛙  ·  [Fixed] ...·  24 分钟前    · 
机灵的楼梯  ·  Python Agent in ...·  24 分钟前    · 
刚分手的鼠标  ·  Setup zmqRemoteApi - ...·  24 分钟前    · 
严肃的钱包  ·  Smartphone remote - ...·  24 分钟前    · 
温柔的围巾  ·  入门使用 · ...·  7 月前    · 

Python's os.path.getctime on the Mac (and under Unix in general) does not give the date when a file was created but "the time of the last change" (according to the docs at least). On the other hand in the Finder I can see the real file creation time so this information is kept by HFS+.

Do you have any suggestions on how to obtain the file creation time on the Mac in a Python program?

Use the st_birthtime property on the result of a call to os.stat() (or fstat/lstat).

def get_creation_time(path):

return os.stat(path).st_birthtime

You can convert the integer result to a datetime object using datetime.datetime.fromtimestamp().

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.

Using ctypes to access 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

Using subprocess to call the stat utility:

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())

Python's os.path.getctime on the Mac (and under Unix in general) does not give the date when a file was created but "the time of the last change" (according to the docs at least). On the other hand in... [root@linux-node1 ~]# stat lnmp-install.log File: `lnmp-install.log' Size: 2228418 Blocks: 4368 IO Block: 4096 regular file Device: fd00h/64768d Inode...
1. 文件 读取的三部曲:打开 ---> 操作 ----> 关闭 r(默认参数):-只能读,不能写- 读取文件 不存在 会报错FileNotFoundError: [Errno 2] No such file or directory: '/tmp/westos' w(写)-write only- 文件 不存在的时候,会自动 创建 新的 文件 - 文件 存在的时候,会清空 文件 内容并写入新的内容 a(追加):-...