用法:
os.fwalk(top='.', topdown=True, onerror=None, *, follow_symlinks=False, dir_fd=None)
它的行為與
walk()
完全相同,隻是它產生一個 4 元組(dirpath, dirnames, filenames, dirfd)
,並且它支持dir_fd
。dirpath
、dirnames
和filenames
與walk()
輸出相同,dirfd
是引用目錄dirpath
的文件說明符。此函數始終支持相對於目錄說明符的路徑,而不是遵循符號鏈接。但是請注意,與其他函數不同,
follow_symlinks
的fwalk()
默認值為False
。這個例子顯示了起始目錄下每個目錄中非目錄文件占用的字節數,除了它不在任何 CVS 子目錄下查找:
import os for root, dirs, files, rootfd in os.fwalk('python/Lib/email'): print(root, "consumes", end="") print(sum([os.stat(name, dir_fd=rootfd).st_size for name in files]), end="") print("bytes in", len(files), "non-directory files") if 'CVS' in dirs: dirs.remove('CVS') # don't visit CVS directories
在下一個示例中,自下而上遍曆樹是必不可少的:
rmdir()
不允許在目錄為空之前刪除目錄:# Delete everything reachable from the directory named in "top", # assuming there are no symbolic links. # CAUTION: This is dangerous! For example, if top == '/', it # could delete all your disk files. import os for root, dirs, files, rootfd in os.fwalk(top, topdown=False): for name in files: os.unlink(name, dir_fd=rootfd) for name in dirs: os.rmdir(name, dir_fd=rootfd)
使用參數
top
、topdown
、onerror
、follow_symlinks
、dir_fd
引發審計事件os.fwalk
。可用性:Unix。
3.3 版中的新函數。
在 3.6 版中更改:接受一個path-like 對象.
在 3.7 版中更改:增加了對
bytes
路徑。
相關用法
- Python os.fork()用法及代碼示例
- Python os.fstatvfs()用法及代碼示例
- Python os.fdatasync()用法及代碼示例
- Python os.fchmod()用法及代碼示例
- Python os.fchdir()用法及代碼示例
- Python os.fsencode()用法及代碼示例
- Python os.ftruncate()用法及代碼示例
- Python os.fsdecode()用法及代碼示例
- Python os.fdopen()用法及代碼示例
- Python os.fsync()用法及代碼示例
- Python os.fchown()用法及代碼示例
- Python os.fstat()用法及代碼示例
- Python os.fpathconf()用法及代碼示例
- Python os.path.normcase()用法及代碼示例
- Python os.read()用法及代碼示例
- Python os.DirEntry.inode()用法及代碼示例
- Python os.closerange()用法及代碼示例
- Python os.set_blocking()用法及代碼示例
- Python os.pathconf()用法及代碼示例
- Python os.chflags()用法及代碼示例
注:本文由純淨天空篩選整理自python.org大神的英文原創作品 os.fwalk。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。