如何阻止os.walk遍历所有子目录?
使用:
def simpleFunc(dirName):
import os
fileList=[]
for dir, dirs, filenames in os.walk(dirName):
for filename in filenames:
filepath=dir+'/'+filename
fileList.append(filepath)
print fileList
simpleFunc(os.path.dirname('/entire/path/to/file.ext'))
问题是,os.walk
这个函数不会停在 /entire/path/to/
这个目录层级,而是会一直往下走,直到找到最底层的子目录为止。所以如果有 /entire/path/to/subdir1/subdir2/subdir3
,那么除了 /entire/path/to/
之外,所有三个子目录也会被搜索:./subdir1/
、././subdir2/
和 ./././subdir3/
。
问题是:如何确保这个函数在指定的目录层级 /entire/path/to/
停下来,而不再往下走呢?
5 个回答
0
你可以明确告诉os.walk要使用哪个目录
def simpleFunc(dirName):
import os
# set the dir name in a var
dirName = '/higher_lever_dir/'
for dir, dirs, filenames in os.walk(dirName):
# this line forces loop to look ONLY in the specified dir
dirs[:] = [d for d in dirs if d in dirName]
for filename in filenames:
filepath=dir+'/'+filename
fileList.append(filepath)
print fileList
simpleFunc(os.path.dirname('/entire/path/to/file.ext'))
把dirs[:]设置为[d for d in dirs if d in dirName],这样就会排除掉在/higher_level_dir/下可能存在的子目录中的文件
0
你需要从os.walk
这个生成器中取出第一个项目:
import os
root, _, files = next(os.walk('/entire/path/to'))
然后把根目录加到每个文件名上:
files = map(lambda f: os.path.join(root, f), files)
或者直接这样做:
files = [os.path.join(root, f) for f in files]
1
听起来你应该使用 os.listdir
来代替。
import os
my_path = '/entire/path/to/files/'
file_list = []
for filename in os.listdir(my_path):
filepath = os.path.join(my_path, filename)
if os.path.isfile(filepath):
fileList.append(filepath)
2
from os import listdir
from os.path import isfile, join
path='/entire/path/to/'
files = [ join(path,f) for f in listdir(path) if isfile(join(path,f)) ]
文件会返回你的文件。没必要再声明一个新的文件列表。
2
根据你写的内容,如果你只是想在指定的文件夹里搜索文件,使用 os.listdir
会更好,然后再用 os.path.isfile
来筛选文件,比如这样:
def simple_func(dirpath):
paths = os.listdir(dirpath)
# create absolute paths
abs_paths = [os.path.join(dirpath, p) for p in paths]
# filter for paths that are files
file_list = [p for p in paths if os.path.isfile(p)]
return file_list
这样你就不需要处理停止递归的问题,而且你想要的文件也很清楚。你可能还想测试一下,看看多次调用 isfile
是否会影响性能。