當我們使用os.stat(path)獲取一個文件(夾)信息的時候,
os.stat(path)本身返回的是一個元組如:
nt.stat_result(st_mode=33206, st_ino=203224933185146561, st_dev=0,
st_nlink=1, st_uid=0, st_gid=0, st_size=21090, st_atime=1376373336,
st_mtime=1376534141, st_ctime=1376373336)
在這個元組中,包含了10個屬性:
st_mode -- protection bits(模式)
st_ino -- inode number(索引號)
st_dev -- device(設備)
st_nlink -- number of hard links(硬鏈接號)
st_uid -- user id of owner(用戶id)
st_gid -- group id of owner (組id)
st_size -- size of file,in bytes (大小)
st_atime -- time of most recent access expressed in seconds (訪問時間)
st_mtime -- time of most recent content modificatin expressed in seconds (修改時間)
st_ctime -- platform dependent;time of most recent metadata change on Unix,
or the teime of creation on Windows,expressed in senconds (根據不同操作系統而定)
#############################################################
而stat在這里起到什么作用呢?
類似於java中定義的一些常量:
如:
os.stat(path).st_size
os.stat(path)[stat.ST_SIZE]
這兩種表示方法是一樣的。
下面是我做的demo:
運行效果:
==============================================
代碼部分:
==============================================
1 #python stat 2 ''' 3 當我們使用os.stat(path)獲取一個文件(夾)信息的時候, 4 os.stat(path)本身返回的是一個元組如: 5 6 nt.stat_result(st_mode=33206, st_ino=203224933185146561, st_dev=0, 7 st_nlink=1, st_uid=0, st_gid=0, st_size=21090, st_atime=1376373336, 8 st_mtime=1376534141, st_ctime=1376373336) 9 10 在這個元組中,包含了10個屬性: 11 st_mode -- protection bits(模式) 12 st_ino -- inode number(索引號) 13 st_dev -- device(設備) 14 st_nlink -- number of hard links(硬鏈接號) 15 st_uid -- user id of owner(用戶id) 16 st_gid -- group id of owner (組id) 17 st_size -- size of file,in bytes (大小) 18 st_atime -- time of most recent access expressed in seconds (訪問時間) 19 st_mtime -- time of most recent content modificatin expressed in seconds (修改時間) 20 st_ctime -- platform dependent;time of most recent metadata change on Unix, 21 or the teime of creation on Windows,expressed in senconds (根據不同操作系統而定) 22 23 ############################################################# 24 而stat在這里起到什么作用呢? 25 類似於java中定義的一些常量: 26 如: 27 os.stat(path).st_size 28 os.stat(path)[stat.ST_SIZE] 29 這兩種表示方法是一樣的。 30 ''' 31 import os 32 import time 33 import stat 34 35 def get_file_stat(path): 36 '''獲取一個文件(夾)信息,該信息將以一個元組的形式返回''' 37 if os.path.exists(path): 38 return os.stat(path) 39 else: 40 print('the path [{}] is not exist!'.format(path)) 41 42 def print_info(file_stat): 43 '''打印信息''' 44 if file_stat != None: 45 file_info = { 46 'Size' : file_stat [ stat.ST_SIZE ], #獲取文件大小 47 'LastModified' : time.ctime( file_stat [ stat.ST_MTIME ] ), #獲取文件最后修改時間 48 'LastAccessed' : time.ctime( file_stat [ stat.ST_ATIME ] ), #獲取文件最后訪問時間 49 'CreationTime' : time.ctime( file_stat [ stat.ST_CTIME ] ), #獲取文件創建時間 50 'Mode' : file_stat [ stat.ST_MODE ], #獲取文件的模式 51 'Device' : file_stat [stat.ST_DEV], #設備 52 'UserID' : file_stat [stat.ST_UID], 53 'GroupID' : file_stat [stat.ST_GID] 54 } 55 for key in file_info: 56 print('{} : {}'.format(key, file_info[key])) 57 else: 58 print('the stat is None!') 59 60 def main(): 61 path_dir = 'c:\\Download' 62 path_file = 'c:\\test.html' 63 print('目錄信息:') 64 file_stat = get_file_stat(path_dir) 65 print_info(file_stat) 66 print('#' * 50) 67 print('文件信息:') 68 file_stat = get_file_stat(path_file) 69 print_info(file_stat) 70 71 if __name__ == '__main__': 72 main() 73