我正在尝试为不同文件夹中具有相同名称和格式的多个文件创建路径列表。我尝试使用os.walk执行此操作,代码如下:
import os
list_raster = []
for (path, dirs, files) in os.walk(r"C:\Users\Douglas\Rasters\Testing folder"):
for file in files:
if "woody02.tif" in file:
list_raster.append(files)
print (list_raster)
然而,这只给了我两件事
我只需要在每个文件夹中指定的'woody02.txt'的完整位置
我做错什么了
在您发布的示例代码中,您将
files
附加到列表中,而不仅仅是当前文件,为了获得当前文件的完整路径和文件名,您需要将代码更改为以下内容:完整路径名是
os.walk
返回的列表中元组的第一项,因此它已经分配给了path
变量更改:
收件人:
相关问题 更多 >
编程相关推荐