如何迭代輸出某文件夾下的所有文件的路徑?(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))

注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2021 ITdaan.com