Python3 獲取文件屬性的方式(時間、大小等)
os.stat(path) :
用于在給定的路徑上執(zhí)行一個系統(tǒng) stat 的調(diào)用。
path:
指定路徑
返回值:
st_mode: inode 保護模式
-File mode: file type and file mode bits (permissions).
st_ino: inode 節(jié)點號。
-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 駐留的設備。
-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: 上次訪問的時間。
-Time of most recent access expressed in seconds.
st_mtime: 最后一次修改的時間。
-Time of most recent content modification expressed in seconds.
st_ctime:由操作系統(tǒng)報告的"ctime"。在某些系統(tǒng)上(如Unix)是最新的元數(shù)據(jù)更改的時間,在其它系統(tǒng)上(如Windows)是創(chuàng)建時間(詳細信息參見平臺的文檔)。
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.
實例:
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ù)-時間戳(每個時間戳都以自從1970年1月1日午夜(歷元)經(jīng)過了多長時間來表示。)
從返回浮點數(shù)的時間輟方式向時間元組轉(zhuǎ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)
附:月份縮寫 -_-||

time 模塊的 strftime 方法來格式化日期
print (time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(statinfo.st_atime)))
結(jié)果:
2018-11-01 08:36:03
附:格式化符號
%y 兩位數(shù)的年份表示(00-99)
%Y 四位數(shù)的年份表示(000-9999)
%m 月份(01-12)
%d 月內(nèi)中的一天(0-31)
%H 24小時制小時數(shù)(0-23)
%I 12小時制小時數(shù)(01-12)
%M 分鐘數(shù)(00=59)
%S 秒(00-59)
%a本地簡化星期名稱
%A 本地完整星期名稱
%b 本地簡化的月份名稱
%B 本地完整的月份名稱
%c 本地相應的日期表示和時間表示
%j年內(nèi)的一天(001-366)
%p 本地A.M.或P.M.的等價符
%U 一年中的星期數(shù)(00-53)星期天為星期的開始
%w星期(0-6),星期天為星期的開始
%W 一年中的星期數(shù)(00-53)星期一為星期的開始
%x 本地相應的日期表示
%X本地相應的時間表示
%Z 當前時區(qū)的名稱
%% %號本身
補充知識:python 獲取請求鏈接下載文件的大小和文件特征
廢話不多說,還只直接看代碼吧!
###根據(jù)url鏈接提取下載文件的大小特征和下載文件類型
def getRemoteFileSize(url, proxy=None):
'''
通過content-length頭獲取遠程文件大小
'''
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:
# 遠程文件不存在
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 獲取文件屬性的方式(時間、大小等)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
pytorch如何自定義forward和backward函數(shù)
PyTorch自動求導功能強大,但在特定情況下需要用戶自行定義backward函數(shù),通過實例解釋了保存變量、計算梯度、鏈式法則等核心概念,并展示了如何通過自定義函數(shù)集成到網(wǎng)絡中以及如何正確返回梯度,此外,還討論了多輸出情況下的梯度傳遞2024-10-10
基于python實現(xiàn)微信好友數(shù)據(jù)分析(簡單)
本文主要介紹利用網(wǎng)頁端微信獲取數(shù)據(jù),實現(xiàn)個人微信好友數(shù)據(jù)的獲取,并進行一些簡單的數(shù)據(jù)分析,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友參考下吧2020-02-02

