这就是我所拥有的:
glob(os.path.join('src','*.c'))
Run Code Online (Sandbox Code Playgroud)
但我想搜索src的子文件夹.像这样的东西会起作用:
glob(os.path.join('src','*.c'))
glob(os.path.join('src','*','*.c'))
glob(os.path.join('src','*','*','*.c'))
glob(os.path.join('src','*','*','*','*.c'))
Run Code Online (Sandbox Code Playgroud)
但这显然是有限和笨重的.
我正在尝试执行一些地理处理.我的任务是在目录中找到所有shapefile,然后在目录中找到该shapefile的完整路径名.我可以获取shapefile的名称,但我不知道如何获取该shapefile的完整路径名.
shpfiles = []
for path, subdirs, files in os.walk(path):
for x in files:
if x.endswith(".shp") == True:
shpfiles.append[x]
Run Code Online (Sandbox Code Playgroud) 您好我在子目录中查找和打开文件时遇到问题.我有几个不同的文件,例如:
mouse_1_animal.txt
mouse_2_animal.txt mouse_3_animal.txt
所以我想在工作目录的子目录中找到所有这些文件并打开它们并使用那些行做一些事情.这是我的尝试:
i=1
for path, subdirs, files in os.walk(root) :
for file in files :
if file == "mouse_{0}_animal.txt".format(i) :
#do something
i = i + 1
Run Code Online (Sandbox Code Playgroud)
但显然它没有找到所有的文件,所以我想知道这是否是我用来找到错误的文件的方式.