python学习笔记之--os.walk使用

os.walk使用:

os.walk()返回一个三元素的tuple:当前路径、子文件夹名称、文件列表。


实例功能:何遍历指定目录下的所有文件

>>> import os
>>> def fun(path):
for root,dirs,file in os.walk(path):
  for fn in file:
   print root+'\\'+fn
   
>>> fun(r'E:\software\share\hadoop-code')
E:\software\share\hadoop-code\current.txt
E:\software\share\hadoop-code\works\code-data\Test_1.java
E:\software\share\hadoop-code\works\code-data\Test_2.java
E:\software\share\hadoop-code\works\code-data\data\Test_1
E:\software\share\hadoop-code\works\code-data\data\Test_2
E:\software\share\hadoop-code\works\work\Exercise_1.java
E:\software\share\hadoop-code\works\work\Exercise_2.java
E:\software\share\hadoop-code\works\work\result_1
E:\software\share\hadoop-code\works\work\result_2
E:\software\share\hadoop-code\works\work\作业.txt  

你可能感兴趣的:(python)