python遍历目录,及参数用法说明
os.walk(“F:\testdemo”,top,topdown=True,οnerrοr=None,followlinks=False)遍历目录
参数说明:
top:表示需要遍历的目录树的路径
topdown的默认值是True,表示首先返回根目录树下的文件,然后遍历目录树下的子目录。值设为False时,则表示先遍历目录树下的子目录,返回子目录下的文件,最后返回根目录下的文件。
试举例说明:
设置topdown = False
import os
import os.path
l = os.walk(“F:\testdemo”,topdown=False)
for i in l:
print(i)
打印结果:
(‘F:\testdemo\aaa’, [], [])
(‘F:\testdemo\bbb’, [], [])
(‘F:\testdemo\ccc’, [], [])
(‘F:\testdemo\xxx’, [], [‘a.txt’, ‘b.txt’, ‘c.txt’])
(‘F:\testdemo’, [‘aaa’, ‘bbb’, ‘ccc’, ‘xxx’], [])
topdown = True
import os
import os.path
l = os.walk(“F:\testdemo”,topdown=True)
for i in l:
print(i)
打印结果:
(‘F:\testdemo’, [‘aaa’, ‘bbb’, ‘ccc’, ‘xxx’], [])
(‘F:\testdemo\aaa’, [], [])
(‘F:\testdemo\bbb’, [], [])
(‘F:\testdemo\ccc’, [], [])
(‘F:\testdemo\xxx’, [], [‘a.txt’, ‘b.txt’, ‘c.txt’])
onerror的默认值是“None”,表示忽略文件遍历时产生的错误。如果不为空,则提供一个自定义函数提示错误信息后继续遍历或抛出异常中止遍历。
该函数返回一个列表,列表中的每一个元素都是一个元组,该元组有3个元素,分别表示每次遍历的路径名,目录列表和文件列表。
import os
import os.path
l = os.walk(“F:\testdemo”,topdown=False)
print(l)
print(list(l))
print(l)打印结果:
<generator object walk at 0x0000024822DA64A0>
print(list(l))
[(‘F:\testdemo\aaa’, [], []), (‘F:\testdemo\bbb’, [], []), (‘F:\testdemo\ccc’, [], []), (‘F:\testdemo\xxx’, [], [‘a.txt’, ‘b.txt’, ‘c.txt’]), (‘F:\testdemo’, [‘aaa’, ‘bbb’, ‘ccc’, ‘xxx’], [])]
默认情况下,os.walk 不会遍历软链接指向子目录,若有需要请将followlinks设定为true
用root,dirs,files三个变量遍历目录的目录层级,目录层级的子目录,目录层级下的文件
for root,dirs,files in os.walk(r"F:\testdemo",topdown=False):
root :表示当前遍历到哪一层级目录了,目录的名字是谁
dirs:表示root下有哪些子目录
files:表示root下边有几个文件
topdown = False:
代码:
import os
import os.path
l = os.walk(“F:\testdemo”,topdown=False)
for i in l:
print(i)
print("*"*50)
for root,dirs,files in os.walk(“F:\testdemo”,topdown=False):
print(“front folder:”,root)
for name in dirs:
print(“folder name:”,os.path.join(root,name))
for name in files:
print(“file name:”,os.path.join(root,name))
打印结果:
(‘F:\testdemo\aaa’, [], [])
(‘F:\testdemo\bbb’, [], [])
(‘F:\testdemo\ccc’, [], [])
(‘F:\testdemo\xxx’, [], [‘a.txt’, ‘b.txt’, ‘c.txt’])
(‘F:\testdemo’, [‘aaa’, ‘bbb’, ‘ccc’, ‘xxx’], [])
front folder: F:\testdemo\aaa
front folder: F:\testdemo\bbb
front folder: F:\testdemo\ccc
front folder: F:\testdemo\xxx
file name: F:\testdemo\xxx\a.txt
file name: F:\testdemo\xxx\b.txt
file name: F:\testdemo\xxx\c.txt
front folder: F:\testdemo
folder name: F:\testdemo\aaa
folder name: F:\testdemo\bbb
folder name: F:\testdemo\ccc
folder name: F:\testdemo\xxx
topdown = True:
代码:
import os
import os.path
l = os.walk(“F:\testdemo”,topdown=True)
for i in l:
print(i)
print("*"*50)
for root,dirs,files in os.walk(“F:\testdemo”,topdown=True):
print(“front folder:”,root)
for name in dirs:
print(“folder name:”,os.path.join(root,name))
for name in files:
print(“file name:”,os.path.join(root,name))
运行结果:
(‘F:\testdemo’, [‘aaa’, ‘bbb’, ‘ccc’, ‘xxx’], [])
(‘F:\testdemo\aaa’, [], [])
(‘F:\testdemo\bbb’, [], [])
(‘F:\testdemo\ccc’, [], [])
(‘F:\testdemo\xxx’, [], [‘a.txt’, ‘b.txt’, ‘c.txt’])
front folder: F:\testdemo
folder name: F:\testdemo\aaa
folder name: F:\testdemo\bbb
folder name: F:\testdemo\ccc
folder name: F:\testdemo\xxx
front folder: F:\testdemo\aaa
front folder: F:\testdemo\bbb
front folder: F:\testdemo\ccc
front folder: F:\testdemo\xxx
file name: F:\testdemo\xxx\a.txt
file name: F:\testdemo\xxx\b.txt
file name: F:\testdemo\xxx\c.txt