Python3 獲取文件屬性的方式(時(shí)間、大小等)
os.stat(path) :
用于在給定的路徑上執(zhí)行一個(gè)系統(tǒng) stat 的調(diào)用。
path:
指定路徑
返回值:
st_mode: inode 保護(hù)模式
-File mode: file type and file mode bits (permissions).
st_ino: inode 節(jié)點(diǎn)號(hào)。
-Platform dependent, but if non-zero, uniquely identifies the file for a given value of st_dev.
——the inode number on Unix,
——the file index on Windows
st_dev: inode 駐留的設(shè)備。
-Identifier of the device on which this file resides.
st_nlink:inode 的鏈接數(shù)。
-Number of hard links.
st_uid: 所有者的用戶ID。
-User identifier of the file owner.
st_gid: 所有者的組ID。
-Group identifier of the file owner.
st_size:普通文件以字節(jié)為單位的大?。话却承┨厥馕募臄?shù)據(jù)。
-Size of the file in bytes, if it is a regular file or a symbolic link. The size of a symbolic link is the length of the pathname it contains, without a terminating null byte.
st_atime: 上次訪問(wèn)的時(shí)間。
-Time of most recent access expressed in seconds.
st_mtime: 最后一次修改的時(shí)間。
-Time of most recent content modification expressed in seconds.
st_ctime:由操作系統(tǒng)報(bào)告的"ctime"。在某些系統(tǒng)上(如Unix)是最新的元數(shù)據(jù)更改的時(shí)間,在其它系統(tǒng)上(如Windows)是創(chuàng)建時(shí)間(詳細(xì)信息參見(jiàn)平臺(tái)的文檔)。
st_atime_ns
-Time of most recent access expressed in nanoseconds as an integer
st_mtime_ns
-Time of most recent content modification expressed in nanoseconds as an integer.
st_ctime_ns
-Platform dependent:
——the time of most recent metadata change on Unix,
——the time of creation on Windows, expressed in nanoseconds as an integer.
實(shí)例:
from os import stat statinfo =stat(r'C:\Users\Administrator\Desktop\1\4D-A300.txt') print (statinfo)#屬性 print(statinfo.st_size) #大小字節(jié) print('%.3f'%(statinfo.st_size/1024/1024))#大小M
輸出結(jié)果:
os.stat_result(st_mode=33206, st_ino=3659174697378650, st_dev=3993776408, st_nlink=1, st_uid=0, st_gid=0, st_size=3876301, st_atime=1541032563, st_mtime=1541033475, st_ctime=1541032563) .697
我們看到,時(shí)間都是一些大的浮點(diǎn)數(shù)-時(shí)間戳(每個(gè)時(shí)間戳都以自從1970年1月1日午夜(歷元)經(jīng)過(guò)了多長(zhǎng)時(shí)間來(lái)表示。)
從返回浮點(diǎn)數(shù)的時(shí)間輟方式向時(shí)間元組轉(zhuǎn)換,只要將浮點(diǎn)數(shù)傳遞給如localtime之類的函數(shù)。
#-*- coding:utf-8 -*- python3.6.3 from os import stat import time statinfo =stat(r'C:\Users\Administrator\Desktop\1\4D-A300.txt') print (statinfo) print(time.localtime(statinfo.st_atime))
輸出為:
os.stat_result(st_mode=33206, st_ino=3659174697378650, st_dev=3993776408, st_nlink=1, st_uid=0, st_gid=0, st_size=3876301, st_atime=1541032563, st_mtime=1541033475, st_ctime=1541032563) time.struct_time(tm_year=2018, tm_mon=11, tm_mday=1, tm_hour=8, tm_min=36, tm_sec=3, tm_wday=3, tm_yday=305, tm_isdst=0)
附:月份縮寫(xiě) -_-||
time 模塊的 strftime 方法來(lái)格式化日期
print (time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(statinfo.st_atime)))
結(jié)果:
2018-11-01 08:36:03
附:格式化符號(hào)
%y 兩位數(shù)的年份表示(00-99)
%Y 四位數(shù)的年份表示(000-9999)
%m 月份(01-12)
%d 月內(nèi)中的一天(0-31)
%H 24小時(shí)制小時(shí)數(shù)(0-23)
%I 12小時(shí)制小時(shí)數(shù)(01-12)
%M 分鐘數(shù)(00=59)
%S 秒(00-59)
%a本地簡(jiǎn)化星期名稱
%A 本地完整星期名稱
%b 本地簡(jiǎn)化的月份名稱
%B 本地完整的月份名稱
%c 本地相應(yīng)的日期表示和時(shí)間表示
%j年內(nèi)的一天(001-366)
%p 本地A.M.或P.M.的等價(jià)符
%U 一年中的星期數(shù)(00-53)星期天為星期的開(kāi)始
%w星期(0-6),星期天為星期的開(kāi)始
%W 一年中的星期數(shù)(00-53)星期一為星期的開(kāi)始
%x 本地相應(yīng)的日期表示
%X本地相應(yīng)的時(shí)間表示
%Z 當(dāng)前時(shí)區(qū)的名稱
%% %號(hào)本身
補(bǔ)充知識(shí):python 獲取請(qǐng)求鏈接下載文件的大小和文件特征
廢話不多說(shuō),還只直接看代碼吧!
###根據(jù)url鏈接提取下載文件的大小特征和下載文件類型 def getRemoteFileSize(url, proxy=None): ''' 通過(guò)content-length頭獲取遠(yuǎn)程文件大小 ''' opener = urllib2.build_opener() if proxy: if url.lower().startswith('https://'): opener.add_handler(urllib2.ProxyHandler({'https' : proxy})) elif url.lower().startswith('http://'): opener.add_handler(urllib2.ProxyHandler({'http' : proxy})) else: opener.add_handler(urllib2.ProxyHandler({'ftp': proxy})) try: request = urllib2.Request(url) request.get_method = lambda: 'HEAD' response = opener.open(request) response.read() except Exception, e: # 遠(yuǎn)程文件不存在 return 0, 0 else: getfileSize = dict(response.headers).get('content-length', 0) filesize = round(float(getfileSize) / 1048576, 2) getContentType = dict(response.headers).get('content-type', 0) return filesize, getContentType
以上這篇Python3 獲取文件屬性的方式(時(shí)間、大小等)就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
抵御代碼復(fù)雜性使python函數(shù)更加Pythonic技巧示例詳解
這篇文章主要介紹了抵御代碼復(fù)雜性使python函數(shù)更加Pythonic技巧示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01(手寫(xiě))PCA原理及其Python實(shí)現(xiàn)圖文詳解
這篇文章主要介紹了Python來(lái)PCA算法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧,希望能給你帶來(lái)幫助2021-08-08scrapy數(shù)據(jù)存儲(chǔ)在mysql數(shù)據(jù)庫(kù)的兩種方式(同步和異步)
這篇文章主要介紹了scrapy數(shù)據(jù)存儲(chǔ)在mysql數(shù)據(jù)庫(kù)的兩種方式(同步和異步),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02Pycharm代碼無(wú)法復(fù)制,無(wú)法選中刪除,無(wú)法編輯的解決方法
今天小編就為大家分享一篇Pycharm代碼無(wú)法復(fù)制,無(wú)法選中刪除,無(wú)法編輯的解決方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-10-10matplotlib 使用 plt.savefig() 輸出圖片去除旁邊的空白區(qū)域
這篇文章主要介紹了matplotlib 使用 plt.savefig() 輸出圖片去除旁邊的空白區(qū)域,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01Python cookbook(數(shù)據(jù)結(jié)構(gòu)與算法)將多個(gè)映射合并為單個(gè)映射的方法
這篇文章主要介紹了Python cookbook(數(shù)據(jù)結(jié)構(gòu)與算法)將多個(gè)映射合并為單個(gè)映射的方法,結(jié)合實(shí)例形式分析了Python字典映射合并操作相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2018-04-04