您的位置:首页 > 编程语言 > Python开发

python-列出所有目录及子目录文件

2018-11-12 16:13 417 查看

列出当前目录及文件

from pathlib import Path
srcPath = Path('../src/')
[x for x in srcPath.iterdir() if srcPath.is_dir()]

列出指定目录及子目录下的所有文件

from pathlib import Path
srcPath = Path('../tensorflow-r1.11/')
allFn=[]
allPath=[srcPath,]
i=1
while len(allPath)>0:
nowPath=allPath.pop()
pathInfo=[(x,x.is_dir()) for x in nowPath.iterdir() if nowPath.is_dir()]
for fn,isPath in pathInfo:
print("正在寻找:","<",str(i),">",fn)
if not isPath:
print("找到新文件:",fn)
allFn.append(fn)
else:
print("找到新目录:",fn)
allPath.append(fn)
i+=1
print(nowPath,end="===>")
print("寻找完毕")
print("寻找完毕,共{}个目录及文件".format(i))

下面这种方式更简洁

list(Path('../tensorflow-r1.11/').glob('/*')
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python3 列出目录