我试图在使用的文件名有时只是完整文件名的一部分的目录中查找文件。
所以
check_meta=os.listdir(currentPath)
给
trc_boundary_Polygon.prj'、'trc_boundary_Polygon.sbn'、'trc_boundary_Polygon.sbx'、'trc_boundary_Polygon.shp'、'trc_boundary_Polygon.shp.ATGIS29.1772.3444.sr.lock'、'trc_boundary_Polygon.shx'、'trc_boundary_gon.' Urbanlevy_bdy_region.dbf'、'Urbanlevy_bdy_region.prj'、'Urbanlevy_bdy_region.shp'、'Urbanlevy_bdy_region.shp.xml'、'Urbanlevy_bdy_region.shx'、'Urbanlevy_bdy_trc.dbf'、'Urbanlevy_bdy_trc。prj','Urbanlevy_bdy_trc.sbn','Urbanlevy_bdy_trc.sbx','Urbanlevy_bdy_trc.shp','Urbanlevy_bdy_trc.shp.xml','Urbanlevy_bdy_trc.shx'] trc_boundary_Polygon.shp.ATGIS29.1772.3444.sr.lock','trc_boundary_Polygon.shx','trc_boundary_polygon.xml','Urbanlevy_bdy_region.dbf','Urbanlevy_bdy_region.prj','Urbanlevy_bdy_region.shp','Urbanlevy_bdy.xml' ,'Urbanlevy_bdy_region.shx','Urbanlevy_bdy_trc.dbf','Urbanlevy_bdy_trc。prj','Urbanlevy_bdy_trc.sbn','Urbanlevy_bdy_trc.sbx','Urbanlevy_bdy_trc.shp','Urbanlevy_bdy_trc.shp.xml','Urbanlevy_bdy_trc.shx'] trc_boundary_Polygon.shp.ATGIS29.1772.3444.sr.lock','trc_boundary_Polygon.shx','trc_boundary_polygon.xml','Urbanlevy_bdy_region.dbf','Urbanlevy_bdy_region.prj','Urbanlevy_bdy_region.shp','Urbanlevy_bdy.xml' ,'Urbanlevy_bdy_region.shx','Urbanlevy_bdy_trc.dbf','Urbanlevy_bdy_trc。prj','Urbanlevy_bdy_trc.sbn','Urbanlevy_bdy_trc.sbx','Urbanlevy_bdy_trc.shp','Urbanlevy_bdy_trc.shp.xml','Urbanlevy_bdy_trc.shx'] shx','Urbanlevy_bdy_trc.dbf','Urbanlevy_bdy_trc。prj','Urbanlevy_bdy_trc.sbn','Urbanlevy_bdy_trc.sbx','Urbanlevy_bdy_trc.shp','Urbanlevy_bdy_trc.shp.xml','Urbanlevy_bdy_trc.shx'] shx','Urbanlevy_bdy_trc.dbf','Urbanlevy_bdy_trc。prj','Urbanlevy_bdy_trc.sbn','Urbanlevy_bdy_trc.sbx','Urbanlevy_bdy_trc.shp','Urbanlevy_bdy_trc.shp.xml','Urbanlevy_bdy_trc.shx']
我想要
existingXML=FileNm[:FileNm.find('.')]
if existingXML+"*"+'.xml' in check_meta: # this is where the issue is
print "exists"
所以有时要使用的 xml 是 Urbanlevy_bdy_trc.shp.xml 而在其他情况下是 Urbanlevy_bdy_trc.xml (以存在者为准 - 请注意,不要简单地对“.shp.xml”使用 OR 函数,因为有多个文件扩展名,如选项卡, ecw 等数据集将具有)。有时,相关的 xml 文件可能称为 Urbanlevy_bdy_trc_Metadata.shp.xml,所以关键是搜索核心文件名“Urbanlevy_bdy_trc”,扩展名为 .xml
我该如何指定?Search and replace multiple lines in xml/text files using python
完整代码
import os, xml, arcpy, shutil, datetime
from xml.etree import ElementTree as et
path=os.getcwd()
RootDirectory=path
arcpy.env.workspace = path
Count=0
Generated_XMLs=RootDirectory+'\GeneratedXML_LOG.txt'
f = open(Generated_XMLs, 'a')
f.write("Log of Metadata Creation Process - Update: "+str(datetime.datetime.now())+"\n")
f.close()
for root, dirs, files in os.walk(RootDirectory, topdown=False):
#print root, dirs
for directory in dirs:
currentPath=os.path.join(root,directory)
os.chdir(currentPath)
arcpy.env.workspace = currentPath
print currentPath
#def Create_xml(currentPath):
FileList = arcpy.ListFeatureClasses()
zone="_Zone"
for File in FileList:
Count+=1
FileDesc_obj = arcpy.Describe(File)
FileNm=FileDesc_obj.file
print FileNm
check_meta=os.listdir(currentPath)
existingXML=FileNm[:FileNm.find('.')]
print "XML: "+existingXML
print check_meta
#if existingXML+'.xml' in check_meta:
if any(f.startswith(existingXML) and f.endswith('.xml') for f in check_meta):
print "exists"
newMetaFile=FileNm+"_2012Metadata.xml"
shutil.copy2(FileNm+'.xml', newMetaFile)
else:
print "Does not exist"
newMetaFile=FileNm+"_BaseMetadata.xml"
shutil.copy2('L:\Data_Admin\QA\Metadata_python_toolset\Master_Metadata.xml', newMetaFile)
tree=et.parse(newMetaFile)
print "Processing: "+str(File)
for node in tree.findall('.//title'):
node.text = str(FileNm)
for node in tree.findall('.//northbc'):
node.text = str(FileDesc_obj.extent.YMax)
for node in tree.findall('.//southbc'):
node.text = str(FileDesc_obj.extent.YMin)
for node in tree.findall('.//westbc'):
node.text = str(FileDesc_obj.extent.XMin)
for node in tree.findall('.//eastbc'):
node.text = str(FileDesc_obj.extent.XMax)
for node in tree.findall('.//native/nondig/formname'):
node.text = str(os.getcwd()+"\\"+File)
for node in tree.findall('.//native/digform/formname'):
node.text = str(FileDesc_obj.featureType)
for node in tree.findall('.//avlform/nondig/formname'):
node.text = str(FileDesc_obj.extension)
for node in tree.findall('.//avlform/digform/formname'):
node.text = str(float(os.path.getsize(File))/int(1024))+" KB"
for node in tree.findall('.//theme'):
node.text = str(FileDesc_obj.spatialReference.name +" ; EPSG: "+str(FileDesc_obj.spatialReference.factoryCode))
print node.text
projection_info=[]
Zone=FileDesc_obj.spatialReference.name
if "GCS" in str(FileDesc_obj.spatialReference.name):
projection_info=[FileDesc_obj.spatialReference.GCSName, FileDesc_obj.spatialReference.angularUnitName, FileDesc_obj.spatialReference.datumName, FileDesc_obj.spatialReference.spheroidName]
print "Geographic Coordinate system"
else:
projection_info=[FileDesc_obj.spatialReference.datumName, FileDesc_obj.spatialReference.spheroidName, FileDesc_obj.spatialReference.angularUnitName, Zone[Zone.rfind(zone)-3:]]
print "Projected Coordinate system"
x=0
for node in tree.findall('.//spdom'):
for node2 in node.findall('.//keyword'):
print node2.text
node2.text = str(projection_info[x])
print node2.text
x=x+1
tree.write(newMetaFile)
f = open(Generated_XMLs, 'a')
f.write(str(Count)+": "+File+"; "+newMetaFile+"; "+currentPath+"\n")
f.close()
# Create_xml(currentPath)
结果