Python3怎么獲取文件屬性-創(chuàng)新互聯(lián)

這篇文章給大家分享的是有關(guān)Python3怎么獲取文件屬性的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過來看看吧。

網(wǎng)站的建設(shè)成都創(chuàng)新互聯(lián)公司專注網(wǎng)站定制,經(jīng)驗(yàn)豐富,不做模板,主營網(wǎng)站定制開發(fā).小程序定制開發(fā),H5頁面制作!給你煥然一新的設(shè)計(jì)體驗(yàn)!已為成都高空作業(yè)車租賃等企業(yè)提供專業(yè)服務(wù)。

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: 上次訪問的時(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ì)信息參見平臺(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)過了多長時(shí)間來表示。)

從返回浮點(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)

附:月份縮寫 -_-||

Python3怎么獲取文件屬性

time 模塊的 strftime 方法來格式化日期

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本地簡化星期名稱
%A 本地完整星期名稱
%b 本地簡化的月份名稱
%B 本地完整的月份名稱
%c 本地相應(yīng)的日期表示和時(shí)間表示
%j年內(nèi)的一天(001-366)
%p 本地A.M.或P.M.的等價(jià)符
%U 一年中的星期數(shù)(00-53)星期天為星期的開始
%w星期(0-6),星期天為星期的開始
%W 一年中的星期數(shù)(00-53)星期一為星期的開始
%x 本地相應(yīng)的日期表示
%X本地相應(yīng)的時(shí)間表示
%Z 當(dāng)前時(shí)區(qū)的名稱
%% %號(hào)本身

補(bǔ)充知識(shí):python 獲取請(qǐng)求鏈接下載文件的大小和文件特征

廢話不多說,還只直接看代碼吧!

###根據(jù)url鏈接提取下載文件的大小特征和下載文件類型
def getRemoteFileSize(url, proxy=None):
  '''
  通過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í)間、大小等)”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)成都網(wǎng)站設(shè)計(jì)公司行業(yè)資訊頻道!

感謝各位的閱讀!關(guān)于“Python3怎么獲取文件屬性”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。

當(dāng)前文章:Python3怎么獲取文件屬性-創(chuàng)新互聯(lián)
轉(zhuǎn)載來于:http://www.muchs.cn/article16/dgisgg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站內(nèi)鏈、網(wǎng)站收錄電子商務(wù)、微信小程序、網(wǎng)站改版、服務(wù)器托管

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站建設(shè)網(wǎng)站維護(hù)公司