我的目标是拥有一个可以在ArcMap中运行的程序,生成缺少数据源名称的.txt文件,然后再次提取(只有数据源名称)用于目录搜索,我已经研究过了os.walk,但我非常困惑。到目前为止,我所完成的是一个可以在ArcMap中运行的程序,用于生成缺少数据源名称和原始路径的.txt文件。我缺少的是能够仅提取数据源名称,并在整个目录中搜索其当前位置,该位置将被写入另一个文本文件。有谁知道这是否可能?到目前为止,我的代码如下:
def execute(self, parameters, messages):
# Identifies broken file names in Table of Contents (TOC)
mxd = arcpy.mapping.MapDocument("current")
brokenlist = arcpy.mapping.ListBrokenDataSources(mxd)
for brokenItem in brokenlist:
arcpy.AddMessage(brokenItem.dataSource)
# Path for writing .csv files and searching
path = r"C:\ENGR597\Project"
# Opens file to write
f = open(path + '/Project_Test.txt', 'w')
# Writes file for arcpy.AddMessage(brokenItem.name)
for brokenItem in brokenlist:
f.write(brokenItem.dataSource)
# Opens text file to be read
f= open('C:\ENGR597\Project\Project_Test.txt', 'r')
# Extracts data source file name out to be used in a search (what I am missing)
# Walks through directory to find file (preliminary code, that I was testing and didn't get far with)
path = r"C:\ENGR597\Project"
def walk_through_files(path, file_name='.pyt'):
for (dirpath, dirnames, filenames) in os.walk(path):
for filename in filenames:
if filename.endswith(file_extension):
yield os.path.join(dirpath, filename)
f = open(path + '/Project_Test2.txt', 'w')
f.write(dirpath, filename)
return
最后,这是使用代码时生成的当前文本文件(只需要为搜索提取的源文件名)的示例:
C:\ENGR597\Project\GIS\Shapefiles\City and Town Points\City and Town
Points.shpC
提前谢谢大家。我从不觉得python会让人头疼。