简体   繁体   中英

Python rename KML file name wit '(' and ')'

I am trying to rename a few KML files with 1) spaces to _ 2) - to _

I am running the script in Arcmap. for the first two it works fine.

But for the ( and ) it's going wrong ?

# Rename file
path = "C:\\DATA\\"
files = os.listdir(path)

for file in files:
  os.rename(os.path.join(path, file), os.path.join(path, file.replace("-", "_")))

for file in files:
  os.rename(os.path.join(path, file), os.path.join(path, file.replace(" ", "_")))

for file in files:
  os.rename(os.path.join(path, file), os.path.join(path, file.replace("(", "_")))

for file in files:
  os.rename(os.path.join(path, file), os.path.join(path, file.replace(")", "_")))

The goals is that I need to import KML files, import them in a Gdb, and then Append them to a excisting layer.

The whole script already works. But I still need to rename the files manually while the gdb will not take in files wit a - minus blancs and ( or ) sign whom are by default in a lot of kml files I have.

Below the whole script that works fine, if i manually rename the - or ( or ) or blanks

# Name: BatchKML_to_GDB.py
# Description: Converts a directory of KMLs and copies the output into a single fGDB.
#              A 2 step process: first convert the KML files, and then copy the featureclases

# Import system models
import arcpy, os

# Rename file
path = "C:\\DATA\\"
files = os.listdir(path)

for file in files:
  os.rename(os.path.join(path, file), os.path.join(path, file.replace(" ", "_")))

for file in files:
  os.rename(os.path.join(path, file), os.path.join(path, file.replace("-", "_")))

# Set workspace (where all the KMLs are)
arcpy.env.workspace= (r"C:\DATA")

# Set local variables and location for the consolidated file geodatabase
outLocation = "C:\\WorkingData\\fGDBs"
MasterGDB = 'AllKMLLayers.gdb'
MasterGDBLocation = os.path.join(outLocation, MasterGDB)

# Create the master FileGeodatabase
arcpy.CreateFileGDB_management(outLocation, MasterGDB)

# Convert all KMZ and KML files found in the current workspace
for kml in arcpy.ListFiles('*.kml'):
  print "CONVERTING: " + os.path.join(arcpy.env.workspace,kml)
  arcpy.KMLToLayer_conversion(kml, outLocation)


# Change the workspace to fGDB location
arcpy.env.workspace = outLocation

# Loop through all the FileGeodatabases within the workspace
wks = arcpy.ListWorkspaces('*', 'FileGDB')
# Skip the Master GDB
wks.remove(MasterGDBLocation)

for fgdb in wks:  

  # Change the workspace to the current FileGeodatabase
  arcpy.env.workspace = fgdb    

  # For every Featureclass inside, copy it to the Master and use the name from the original fGDB  
  featureClasses = arcpy.ListFeatureClasses('*', '', 'Placemarks')
  for fc in featureClasses:
    print "COPYING: " + fc + " FROM: " + fgdb    
    fcCopy = fgdb + os.sep + 'Placemarks' + os.sep + fc    
    arcpy.FeatureClassToFeatureClass_conversion(fcCopy, MasterGDBLocation, fgdb[fgdb.rfind(os.sep)+1:-4])


# Clean up
del kml, wks, fc, featureClasses, fgdb

It should be obvious why this doesn't work - once one of your loops has actually renamed a file, the subsequent loops won't be able to find it, because they're still looking for the original name!

You need a SINGLE loop, with all of the .replace() operations chained together so that the file gets renamed directly to its final name.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM