码迷,mamicode.com
首页 > 编程语言 > 详细

python-os.walk()使用举例

时间:2016-07-22 14:22:55      阅读:125      评论:0      收藏:0      [点我收藏+]

标签:

 

 

文件目录结构

 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

python-os.walk()使用举例

标签:

原文地址:http://www.cnblogs.com/moonpool/p/5694880.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!