[Python]擷取檔案屬性__Python

來源:互聯網
上載者:User

很多時候,我們需要擷取一個檔案的屬性,比如建立日期,訪問日期,修改日期,大小 ,唯讀還是隱藏等屬性。用python是相當的方便。下面是我通過查資料得到的方法:

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

  >>>   import  os
>>>  statinfo = os.stat(r " C:/1.txt " )
>>>  statinfo
( 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函數可以知道: >>>   import  time
>>>  time.localtime(statinfo.st_ctime)
( 2008 ,  2 ,  1 ,  19 ,  30 ,  13 ,  4 ,  32 , 0)

看到了吧,是2008年2月1日的19時30分13秒(2008-2-1 19:30:13),呵呵

另外,statinfo的屬性還有:

st_size (檔案大小,以位元組表示)

以上在windows xp上測試。不盡之處,請查閱python手冊。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.