python基础教程: os.stat() 和 stat模块详解
生活随笔
收集整理的這篇文章主要介紹了
python基础教程: os.stat() 和 stat模块详解
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
stat 系統(tǒng)調用時用來返回相關文件的系統(tǒng)狀態(tài)信息的。
首先我們看一下stat中有哪些屬性:
>>> import os >>> print (os.stat("/root/python/zip.py")) (33188, 2033080, 26626L, 1, 0, 0, 864, 1297653596, 1275528102, 1292892895) >>> print (os.stat("/root/python/zip.py").st_mode) #權限模式 33188 >>> print (os.stat("/root/python/zip.py").st_ino) #inode number 2033080 >>> print (os.stat("/root/python/zip.py").st_dev) #device 26626 >>> print (os.stat("/root/python/zip.py").st_nlink) #number of hard links 1 >>> print (os.stat("/root/python/zip.py").st_uid) #所有用戶的user id 0 >>> print (os.stat("/root/python/zip.py").st_gid) #所有用戶的group id 0 >>> print (os.stat("/root/python/zip.py").st_size) #文件的大小,以位為單位 864 >>> print (os.stat("/root/python/zip.py").st_atime) #文件最后訪問時間 1297653596 >>> print (os.stat("/root/python/zip.py").st_mtime) #文件最后修改時間 1275528102 >>> print (os.stat("/root/python/zip.py").st_ctime) #文件創(chuàng)建時間 1292892895正如你上面看到的,你可以直接訪問到這些屬性值。
好了,下面我來看看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_modeif S_ISDIR(mode):# It's a directory, recurse into itwalktree(pathname, callback)elif S_ISREG(mode):# It's a file, call the callback functioncallback(pathname)else:# Unknown file type, print a messageprint ('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結果的函數(shù),就想上面的例子中的一樣,這些函數(shù)可以做出判斷:
''' 遇到問題沒人解答?小編創(chuàng)建了一個Python學習交流QQ群:531509025 尋找有志同道合的小伙伴,互幫互助,群里還有不錯的視頻學習教程和PDF電子書! ''' 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.') ##額外的兩個函數(shù) stat.S_IMODE (mode): #返回文件權限的chmod格式print ('chmod format.')stat.S_IFMT (mode): #返回文件的類型print ('type of fiel.')2.還有一些是各種各樣的標示符,這些標示符也可以在os.chmod中使用,下面附上這些標示符的說明:
| stat.S_ISGID: Set group ID on execution. | 不常用 |
| stat.S_ENFMT: Record locking enforced. | 不常用 |
| stat.S_ISVTX: Save text image after execution. | 在執(zhí)行之后保存文字和圖片 |
| stat.S_IREAD: Read by owner. | 對于擁有者讀的權限 |
| stat.S_IWRITE: Write by owner. | 對于擁有者寫的權限 |
| stat.S_IEXEC: Execute by owner. | 對于擁有者執(zhí)行的權限 |
| stat.S_IRWXU: Read, write, and execute by owner. | 對于擁有者讀寫執(zhí)行的權限 |
| stat.S_IRUSR: Read by owner. | 對于擁有者讀的權限 |
| stat.S_IWUSR: Write by owner. | 對于擁有者寫的權限 |
| stat.S_IXUSR: Execute by owner. | 對于擁有者執(zhí)行的權限 |
| stat.S_IRWXG: Read, write, and execute by group. | 對于同組的人讀寫執(zhí)行的權限 |
| stat.S_IRGRP: Read by group. | 對于同組讀的權限 |
| stat.S_IWGRP: Write by group. | 對于同組寫的權限 |
| stat.S_IXGRP: Execute by group. | 對于同組執(zhí)行的權限 |
| stat.S_IRWXO: Read, write, and execute by others. | 對于其他組讀寫執(zhí)行的權限 |
| stat.S_IROTH: Read by others. | 對于其他組讀的權限 |
| stat.S_IWOTH: Write by others. | 對于其他組寫的權限 |
| stat.S_IXOTH: Execute by others. | 對于其他組執(zhí)行的權限 |
例子:我想獲得某個文件的屬性信息,并查看他的權限信息,用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 與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的python基础教程: os.stat() 和 stat模块详解的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python基础教程:对象之间的交互
- 下一篇: Python基础教程:list深拷贝和浅