root,dir,file的循环是os.walk(startdir)通过这些步骤工作的吗?
for root in os.walk(startdir)
for dir in root
for files in dir
Run Code Online (Sandbox Code Playgroud)
得到启动目录的根目录:C:\ dir1\dir2\startdir
获取C:\ dir1\dir2\startdir中的文件夹并返回文件夹列表"dirlist"
获取第一个dirlist项目中的文件并返回文件列表"filelist"作为文件列表列表的第一项.
移动到dirlist中的第二个项目并返回此文件夹"filelist2"中的文件列表作为文件列表列表的第二项.等等
移动到foldertree的下一个根,从2开始.
对?或者它首先得到所有的根,然后所有的dirs第二和所有文件第三?
Cir*_*四事件 45
最小的可运行示例
这就是我喜欢学习的东西:
mkdir root
cd root
mkdir \
d0 \
d1 \
d0/d0_d1
touch \
f0 \
d0/d0_f0 \
d0/d0_f1 \
d0/d0_d1/d0_d1_f0
tree
Run Code Online (Sandbox Code Playgroud)
输出:
.
??? d0
? ??? d0_d1
? ? ??? d0_d1_f0
? ??? d0_f0
? ??? d0_f1
??? d1
??? f0
Run Code Online (Sandbox Code Playgroud)
主文件
#!/usr/bin/env python3
import os
for path, dirnames, filenames in os.walk('root'):
print('{} {} {}'.format(repr(path), repr(dirnames), repr(filenames)))
Run Code Online (Sandbox Code Playgroud)
输出:
'root' ['d0', 'd1'] ['f0']
'root/d0' ['d0_d1'] ['d0_f0', 'd0_f1']
'root/d0/d0_d1' [] ['d0_d1_f0']
'root/d1' [] []
Run Code Online (Sandbox Code Playgroud)
这让一切都清楚了:
path 是每一步的根目录dirnames 是每个目录基名的列表 pathfilenames 是每个文件基名的列表 path在 Ubuntu 16.04、Python 3.5.2 上测试。
修改dirnames改变树递归
这基本上是您唯一需要记住的其他事情。
例如,如果对 进行以下操作dirnames,则会影响遍历:
走文件或目录
如果遍历的输入是文件或目录,您可以这样处理:
#!/usr/bin/env python3
import os
import sys
def walk_file_or_dir(root):
if os.path.isfile(root):
dirname, basename = os.path.split(root)
yield dirname, [], [basename]
else:
for path, dirnames, filenames in os.walk(root):
yield path, dirnames, filenames
for path, dirnames, filenames in walk_file_or_dir(sys.argv[1]):
print(path, dirnames, filenames)
Run Code Online (Sandbox Code Playgroud)
Sam*_*lar 31
os.walk 返回一个生成器,它创建一个值元组(current_path,current_path中的目录,current_path中的文件).
每次调用生成器时,它将递归地跟随每个目录,直到调用walk的初始目录中没有其他子目录可用.
因此,
os.walk('C:\dir1\dir2\startdir').next()[0] # returns 'C:\dir1\dir2\startdir'
os.walk('C:\dir1\dir2\startdir').next()[1] # returns all the dirs in 'C:\dir1\dir2\startdir'
os.walk('C:\dir1\dir2\startdir').next()[2] # returns all the files in 'C:\dir1\dir2\startdir'
Run Code Online (Sandbox Code Playgroud)
所以
import os.path
....
for path, directories, files in os.walk('C:\dir1\dir2\startdir'):
if file in files:
print 'found %s' % os.path.join(path, file)
Run Code Online (Sandbox Code Playgroud)
或这个
def search_file(directory = None, file = None):
assert os.path.isdir(directory)
for cur_path, directories, files in os.walk(directory):
if file in files:
return os.path.join(directory, cur_path, file)
return None
Run Code Online (Sandbox Code Playgroud)
或者如果你想查找文件,你可以这样做:
import os
def search_file(directory = None, file = None):
assert os.path.isdir(directory)
current_path, directories, files = os.walk(directory).next()
if file in files:
return os.path.join(directory, file)
elif directories == '':
return None
else:
for new_directory in directories:
result = search_file(directory = os.path.join(directory, new_directory), file = file)
if result:
return result
return None
Run Code Online (Sandbox Code Playgroud)
我的回答非常基本且简单。我自己是一名初学者,通过搜索网络找到了答案(尤其是docs.python.org 上的优秀文档)并尝试了一些测试代码,例如:
\nfor root, dirs, files in os.walk(startdir)\n print ("__________________")\n print (root)\n for file in files:\n print ("---",file)\nRun Code Online (Sandbox Code Playgroud)\n这会打印出目录树,其中每个 dir\xe2\x80\x94(起始目录和包含的子目录\xe2\x80\x94)前面有一行,后面是其中包含的文件。
\n我认为你必须记住两件事:
\n(1) os.walk 生成一个 3 元组(三元组)<root,dirs,filenames> 其中
\nroot 是包含根目录名称的字符串;
\ndirs 是一个字符串列表:直接包含在 root 中的目录名称,即第一级目录,其中可能不包含子目录;
\nfilenames 是一个字符串列表:直接包含在 root 中的文件名。
\n(2) for 循环,例如
\nfor root, subdirs, files in os.walk(YourStartDir)\nRun Code Online (Sandbox Code Playgroud)\n循环遍历根目录及其所有子目录。它不需要为每个文件执行一个步骤;它只是扫描目录树,并在每一步(对于树中的每个目录)填充其中包含的文件名列表以及直接包含在其中的子目录列表。如果有n个目录(包括根目录及其子目录),则for循环循环n次,即需要n步。您可以编写一小段测试代码来检查这一点,例如使用计数器。\n在每一步,它都会生成一个三元组:一个字符串加上两个(可能为空)字符串列表。\n在此示例中, 3元组称为:“root”、“subdirs”、“files”,但这些名称由您决定;如果你的代码是
\nfor a, b, c in os.walk(startdir)\nRun Code Online (Sandbox Code Playgroud)\n三元组的元素将被称为“a”、“b”、“c”。
\n我们回到测试代码:
\nfor root, dirs, files in os.walk(startdir)\n print ("__________________")\n print (root)\n for file in files:\n print ("---",file)\nRun Code Online (Sandbox Code Playgroud)\n第一个循环: root 是您在输入中给出的目录(起始路径,起始目录:字符串),dirs 是包含的子目录名称的列表(但不是其中包含的目录的名称),files 是包含的文件列表。在测试代码中,我们没有使用列表“dirs”。
\n第二个循环:root 现在是第一个子目录,dirs 是其中包含的子目录的列表,files 是其中包含的文件的列表。
\n...依此类推,直到到达树中的最后一个子目录。
\nos.walk 有三个可选参数:您可以在网上找到很多有关它们及其使用的信息,但我认为您的问题是关于 os.walk 的基础知识。
\nos.walk 的工作方式与上面略有不同。基本上,它返回(路径、目录、文件)的元组。要查看此内容,请尝试以下操作:
import pprint
import os
pp=pprint.PrettyPrinter(indent=4)
for dir_tuple in os.walk("/root"):
pp.pprint(dir_tuple)
Run Code Online (Sandbox Code Playgroud)
...您将看到循环的每次迭代都会打印一个目录名称、该目录中紧邻的任何目录的名称列表以及该目录中所有文件的另一个列表。然后 os.walk 将进入子目录列表中的每个目录并执行相同的操作,直到遍历完原始根目录的所有子目录。了解一些有关递归的知识可能有助于理解其工作原理。
这是os.walk()如何工作的一个简短示例,并使用一些os函数进行了一些解释。
首先请注意os.walk()返回三个项目,根目录、当前根目录下的目录列表 ( dirs) 以及在这些目录中找到的文件列表。该文档将为您提供更多信息。
dirs将包含根目录下的目录列表,文件将包含在这些目录中找到的所有文件的列表。在下一次迭代中,上一个dirs列表中的每个目录将root依次承担起搜索的作用,并从那里继续搜索,只有在搜索到当前级别后才向下一级。
代码示例:这将搜索、计算和打印指定搜索目录(您的根目录)下的.jpg 和.gif文件的名称。它还利用os.path.splitext()函数将文件的基础与其扩展名分开,并使用os.path.join()函数为您提供全名,包括找到的图像文件的路径。
import os
searchdir = r'C:\your_root_dir' # your search starts in this directory (your root)
count = 0
for root, dirs, files in os.walk(searchdir):
for name in files:
(base, ext) = os.path.splitext(name) # split base and extension
if ext in ('.jpg', '.gif'): # check the extension
count += 1
full_name = os.path.join(root, name) # create full path
print(full_name)
print('\ntotal number of .jpg and .gif files found: %d' % count)
Run Code Online (Sandbox Code Playgroud)
简单来说,os.walk()将生成路径,文件夹,文件存在于给定路径中的元组,并将继续遍历子文件夹.
import os.path
path=input(" enter the path\n")
for path,subdir,files in os.walk(path):
for name in subdir:
print os.path.join(path,name) # will print path of directories
for name in files:
print os.path.join(path,name) # will print path of files
Run Code Online (Sandbox Code Playgroud)
这将生成子目录中所有子目录,文件和文件的路径