我试图使用变量和循环来打开函数内的多个文件。我有的是:
import os
import arcpy.geoprocessing
path = r"F:Earth2018Bands\"
files = os.listdir(path)
RowColumn = []
for TIF in files:
firstsix = TIF[0:6]
RowColumn.append(firstsix)
RowColumn = list(dict.fromkeys(RowColumn))
year= ['2013', '2014', '2015', '2016', '2017', '2018', '2019', '2020']
for code in RowColumn:
with arcpy.EnvManager(scratchWorkspace=r"F:Earth'year'project_'year'project_'year'.gdb",
workspace=r"F:Earth'year'project_'year'project_'year'.gdb"):
arcpy.management.CompositeBands(
r"F:Earth'year'Bands'code'_'year'_B2.TIF;"
r"F:Earth'year'Bands'code'_'year'_B3.TIF;"
r"F:Earth'year'Bands'code'_'year'_B4.TIF;"
r"F:Earth'year'Bands'code'_'year'_B5.TIF;"
r"F:Earth'year'Bands'code'_'year'_B6.TIF;"
r"F:Earth'year'Bands'code'_'year'_B7.TIF",
r"F:Earth'year'project_'year'project_'year'.gdba'code'_'year'")
我将每个文件重命名为" 042024_2017_b2 . tiff "因此,前6个数字是TIF文件位置的代码,然后是中间的年份,然后是频带编号。我每年有大约80张图片,每张图片有6个波段。RowNames在打印时打印完美(所有6位数代码作为文件名的前6位)。
我需要将RowNames中的项插入到它说'code'的地方,并且我需要将年份插入到我放入'year'的地方。我该怎么做呢?
下面的代码给出了错误:错误000732:输入光栅:数据集F:地球'年'波段'代码'_'年'_B2.TIF不存在或不受支持错误000354:名称包含无效字符执行失败(CompositeBands).
所以它不能识别我试图赋值的变量名但是我不知道如何解决这个问题
编辑:
这是代码,最终工作与一个很大的感谢@AlexK:
import os
import arcpy.geoprocessing
years = ['2013', '2014', '2015', '2016', '2017', '2018', '2019', '2020']
print(years)
#Renaming the files
for year in years:
path = fr"F:Earth{year}Bands\"
files = os.listdir(path)
# rename the files
for file in files:
src = path + file
code = file[10:21]
band = file[40:]
dst = path + code + band
os.renames(src, dst)
for year in years:
#Setting the path again, likely redundant
path = fr"F:Earth{year}Bands\"
files = os.listdir(path)
#Make a list of the unique codes
RowColumn = []
for TIF in files:
firstsix = TIF[0:6]
RowColumn.append(firstsix)
RowColumn = list(dict.fromkeys(RowColumn))
print(RowColumn)
# Make a loop to run the functions for each code
for code in RowColumn:
# Make the Composite Bands (one photo with all the layers)
with arcpy.EnvManager(scratchWorkspace=fr"F:Earth{year}project_{year}project_{year}.gdb",
workspace=fr"F:Earth{year}project_{year}project_{year}.gdb"):
arcpy.management.CompositeBands(
fr"F:Earth{year}Bands{code}_{year}_B2.TIF;"
fr"F:Earth{year}Bands{code}_{year}_B3.TIF;"
fr"F:Earth{year}Bands{code}_{year}_B4.TIF;"
fr"F:Earth{year}Bands{code}_{year}_B5.TIF;"
fr"F:Earth{year}Bands{code}_{year}_B6.TIF;"
fr"F:Earth{year}Bands{code}_{year}_B7.TIF",
fr"F:Earth{year}project_{year}project_{year}.gdba{code}_{year}")
# This copies and deletes the null values, so I can mosaic them together.
with arcpy.EnvManager(scratchWorkspace=fr"F:Earth{year}project_{year}project_{year}.gdb",
workspace=fr"F:Earth{year}project_{year}project_{year}.gdb"):
arcpy.management.CopyRaster(fr"a{code}_{year}",
fr"F:Earth{year}project_{year}project_{year}.gdbc{code}_{year}", '', 0,
"0", "NONE", "NONE", "16_BIT_UNSIGNED", "NONE", "NONE", "TIFF", "NONE",
"CURRENT_SLICE", "NO_TRANSPOSE")
# Once it is here, I want it to delete the bands from the bands folder and the composite bands
for fname in files:
if fname.startswith(code):
os.remove(os.path.join(path, fname))
print({code})
r
字面值前缀只能应用于字符串字面值,因此您将无法在那里插入变量名。如果你有Python 3.6+,你可以将所有这些字符串转换为原始的f-string。
(首先,您应该将year
变量更改为字符串:year = '2018'
。)
之后,尝试:
for code in RowColumn:
with arcpy.EnvManager(scratchWorkspace=fr"F:Earth{year}project_{year}project_{year}.gdb",
workspace=fr"F:Earth{year}project_{year}project_{year}.gdb"):
arcpy.management.CompositeBands(
fr"F:Earth{year}Bands{code}_{year}_B2.TIF;"
fr"F:Earth{year}Bands{code}_{year}_B3.TIF;"
fr"F:Earth{year}Bands{code}_{year}_B4.TIF;"
fr"F:Earth{year}Bands{code}_{year}_B5.TIF;"
fr"F:Earth{year}Bands{code}_{year}_B6.TIF;"
fr"F:Earth{year}Bands{code}_{year}_B7.TIF",
fr"F:Earth{year}project_{year}project_{year}.gdba{code}_{year}")