天天看點

Python 擷取檔案屬性

檔案屬性的擷取,使用了os.stat() 方法:

Python 擷取檔案屬性

>>> import os

Python 擷取檔案屬性

>>> statinfo=os.stat(r"C:\1.txt")

Python 擷取檔案屬性

>>> statinfo

Python 擷取檔案屬性

(33206, 0L, 0, 0, 0, 0, 29L, 1201865413, 1201867904, 1201865413)

使用os.stat的傳回值statinfo的三個屬性擷取檔案的建立時間等

st_atime (通路時間), st_mtime (修改時間), st_ctime(建立時間),例如,取得檔案建立時間:

>>> statinfo.st_ctime

1201865413.8952832

為什麼是這樣一個大的浮點數啊?這個時間是什麼意思?

最近的學習得知,這個就是從1970-1-1 08:00:00開始的“秒數”,也就是說,這個時間就是從1970-1-1 08:00:00開始,過了1201865413.8952832秒之後的時間。那這個時間到底是什麼時間呢?

使用time子產品中的localtime函數可以知道:

Python 擷取檔案屬性

>>> import time

Python 擷取檔案屬性

>>> time.localtime(statinfo.st_ctime)

Python 擷取檔案屬性

(2008, 2, 1, 19, 30, 13, 4, 32, 0)