概述
os.stat() 方法用於在給定的路徑上執行一個系統 stat 的調用。
語法
stat()方法語法格式如下:
os.stat(path)
參數
path -- 指定路徑
返回值
stat 結構:
st_mode: inode 保護模式
st_ino: inode 節點號。
st_dev: inode 駐留的設備。
st_nlink: inode 的鏈接數。
st_uid: 所有者的用戶ID。
st_gid: 所有者的組ID。
st_size: 普通文件以字節為單位的大小;包含等待某些特殊文件的數據。
st_atime: 上次訪問的時間。
st_mtime: 最后一次修改的時間。
st_ctime: 由操作系統報告的"ctime"。在某些系統上(如Unix)是最新的元數據更改的時間,在其它系統上(如Windows)是創建時間(詳細信息參見平台的文檔)。
實例
以下實例演示了 stat() 方法的使用:
#!/usr/bin/python3
import os, sys
# 顯示文件 "a2.py" 信息
statinfo = os.stat('a2.py')
print (statinfo)
執行以上程序輸出結果為:
posix.stat_result(st_mode=33188, st_ino=3940649674337682L, st_dev=277923425L, st
_nlink=1, st_uid=400, st_gid=401, st_size=335L, st_atime=1330498089, st_mtime=13
30498089, st_ctime=1330498089)
正如你上面看到的,你可以直接訪問到這些屬性值。
好了,下面我來看看python中的stat模塊,先看看自帶的例子:
import os, sys
from stat import * def walktree(top, callback): '''recursively descend the directory tree rooted at top, calling the callback function for each regular file''' for f in os.listdir(top): pathname = os.path.join(top, f) mode = os.stat(pathname).st_mode if S_ISDIR(mode): # It's a directory, recurse into it walktree(pathname, callback) elif S_ISREG(mode): # It's a file, call the callback function callback(pathname) else: # Unknown file type, print a message print 'Skipping %s' % pathname def visitfile(file): print 'visiting', file if __name__ == '__main__': walktree(sys.argv[1], visitfile)
可以這么理解,os.stat是將文件的相關屬性讀出來,然后用stat模塊來處理,處理方式有多重,就要看看stat提供了什么了。
1. 可以對st_mode做相關的判斷,如是否是目錄,是否是文件,是否是管道等。
先看一下處理os.stat返回的st_mode結果的函數,就想上面的例子中的一樣,這些函數可以做出判斷:
if stat.S_ISREG(mode): #判斷是否一般文件 print 'Regular file.' elif stat.S_ISLNK (mode): #判斷是否鏈接文件 print 'Shortcut.' elif stat.S_ISSOCK (mode): #判斷是否套接字文件 print 'Socket.' elif stat.S_ISFIFO (mode): #判斷是否命名管道 print 'Named pipe.' elif stat.S_ISBLK (mode): #判斷是否塊設備 print 'Block special device.' elif stat.S_ISCHR (mode): #判斷是否字符設置
print 'Character special device.'
elif stat.S_ISDIR (mode): #判斷是否目錄
print 'directory.'
##額外的兩個函數
stat.S_IMODE (mode): #返回文件權限的chmod格式
print 'chmod format.'
stat.S_IFMT (mode): #返回文件的類型
print 'type of fiel.'
2. 還有一些是各種各樣的標示符,這些標示符也可以在os.chmod中使用,下面附上這些標示符的說明:
stat.S_ISUID: Set user ID on execution. 不常用
stat.S_ISGID: Set group ID on execution. 不常用
stat.S_ENFMT: Record locking enforced. 不常用
stat.S_ISVTX: Save text image after execution. 在執行之后保存文字和圖片
stat.S_IREAD: Read by owner. 對於擁有者讀的權限
stat.S_IWRITE: Write by owner. 對於擁有者寫的權限
stat.S_IEXEC: Execute by owner. 對於擁有者執行的權限
stat.S_IRWXU: Read, write, and execute by owner. 對於擁有者讀寫執行的權限
stat.S_IRUSR: Read by owner. 對於擁有者讀的權限
stat.S_IWUSR: Write by owner. 對於擁有者寫的權限
stat.S_IXUSR: Execute by owner. 對於擁有者執行的權限
stat.S_IRWXG: Read, write, and execute by group. 對於同組的人讀寫執行的權限
stat.S_IRGRP: Read by group. 對於同組讀的權限
stat.S_IWGRP: Write by group. 對於同組寫的權限
stat.S_IXGRP: Execute by group. 對於同組執行的權限
stat.S_IRWXO: Read, write, and execute by others. 對於其他組讀寫執行的權限
stat.S_IROTH: Read by others. 對於其他組讀的權限
stat.S_IWOTH: Write by others. 對於其他組寫的權限
stat.S_IXOTH: Execute by others. 對於其他組執行的權限
例子:我想獲得某個文件的屬性信息,並查看他的權限信息,用chmod的格式顯示出來。
復制代碼
>>> import stat
>>> import os
>>> st = os.stat('sig.txt')
>>> mode = st.st_mode
>>> stat.S_IFMT(mode)
32768
>>> stat.S_IMODE(mode)
438
>>> print oct(stat.S_IMODE(mode))#oct 是轉換為八進制
0666