2009-06-03 120 views
8

Mac上(以及一般Unix下)的Python os.path.getctime不會給出文件創建的日期,而是「最後一次更改的時間」(根據文檔至少)。另一方面,在Finder中,我可以看到真正的文件創建時間,因此這些信息由HFS +保存。在Mac上使用Python獲取文件創建時間

對於如何在Python程序中獲取Mac上的文件創建時間,您有什麼建議嗎?

+0

重複:HTTP:/ /stackoverflow.com/questions/237079/how-to-get-file-creation-modification-date-times-in-python – 2009-06-03 21:20:36

+1

@ S.Lott :並非如此,因爲在Mac上獲取文件創建時間本質上是非跨平臺的。 – Miles 2009-06-03 21:25:56

+0

@Miles:也許這是真的,但那裏的答案完全適用於這個問題。 – 2009-06-03 21:29:28

回答

14

使用st_birthtime財產上os.stat()(或fstat/lstat)呼叫的結果。

def get_creation_time(path): 
    return os.stat(path).st_birthtime 

您可以使用datetime.datetime.fromtimestamp()將整數結果轉換爲日期時間對象。

出於某種原因,我不認爲這在第一次寫這個答案時適用於Mac OS X,但我可能會誤解,現在它甚至可以使用舊版本的Python。舊的答案在後面。


使用​​訪問系統調用stat64(與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調用stat實用程序:

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

完美!這正是我所期待的。 – cefstat 2009-06-03 21:23:07

+1

在El Capitan上,如果你得到一個'OSError:dlopen(),你可能想用'libc = CDLL('/ usr/lib/libc.dylib')'而不是'libc = CDLL('libc.dylib')' libc.dylib,6):找不到圖像。 – 2015-10-07 08:43:51

+0

@ThomasOrozco總而言之,有一種比ctypes更好的方法。 – Miles 2015-10-08 01:34:18

1

ctime在平臺上有所不同:某些系統(如Unix)是最後一次元數據更改的時間,在其他系統(如Windows)上,創建時間爲。這是因爲Unices通常不會保留「原創」創作時間。

這就是說你可以訪問操作系統提供的所有信息,stat模塊。

The stat module defines constants and functions for interpreting the results of os.stat(), os.fstat() and os.lstat() (if they exist). For complete details about the stat, fstat and lstat calls, consult the documentation for your system.

stat.ST_CTIME
The 「ctime」 as reported by the operating system. On some systems (like Unix) is the time of the last metadata change, and, on others (like Windows), is the creation time (see platform documentation for details).

+0

謝謝但... stat.ST_CTIME:操作系統報告的「ctime」。在某些系統上(如Unix)是最後一次元數據更改的時間,而在其他系統上(如Windows)則是創建時間(有關詳細信息,請參閱平臺文檔)。 – cefstat 2009-06-03 20:23:22

+1

@cefstat就是這一點。有些系統(如unices)只是不提供「原始」創建時間。 Python沒有辦法做到這一點。 – lothar 2009-06-03 20:25:20

相關問題