如何迭代输出某文件夹下的所有文件的路径?(os.walk用法)

查看目录结构: tree

查看文件结构: os.walk

查看os.walk用法: help(os.walk)

For each directory in the directory tree rooted at top (including top itself, but excluding '.' and '..'), yields a 3-tuple dirpath, dirnames, filenames

dirpath is a string, the path to the directory. dirnames is a list of the names of the subdirectories in dirpath (excluding '.' and '..').

filenames is a list of the names of the non-directory files in dirpath. Note that the names in the lists are just names, with no path components.

To get a full path (which begins with top) to a file or directory in dirpath, do os.path.join(dirpath, name).

根据以上的内容,发现可以迭代输出文件下的所有文件的路径地址:

import os
for root,dirs,files in os.walk("/home/cbb/桌面/"):
for dir in dirs:
for file in files:
print(os.path.join(root,dir,file))
上一篇:error while obtaining ui hierarchy xml file...用 uiautomatorviewer 获取安卓手机软件页面时报错


下一篇:用户不在sudoers 文件中。此事将被报告 or (usermod:“sudo”组不存在)