文件目录结构
1 dir 2 1 3 1 4 1.txt 5 2.txt 6 3.txt 7 2 8 2.txt 9 3 10 4 11 4.txt 12 3.txt 13 1.txt 14 2 15 2.txt 16 3 17 3.txt 18 dir.txt
代码:
1 import os 2 3 def main(): 4 #InputDir="D:\python\dir" 5 InputDir="dir" 6 print(InputDir) 7 for root,dirs,files in os.walk(InputDir): 8 for filename in files: 9 #fullPath=os.path.join(dirpath, filename) 10 fullFilename = root +‘/‘ + filename 11 print(fullFilename) 12 13 14 main()
执行结果:
1 dir 2 dir/dir.txt 3 dir\1/1.txt 4 dir\1\1/1.txt 5 dir\1\1/2.txt 6 dir\1\1/3.txt 7 dir\1\2/2.txt 8 dir\1\3/3.txt 9 dir\1\3\4/4.txt 10 dir\2/2.txt 11 dir\3/3.txt
从结果中可以看到它是按照深度方式遍历文件的。
参考:
http://blog.csdn.net/b_h_l/article/details/11241841
时间: 2024-10-03 08:25:12