I have a script that renames and copies files (feature classes) on an SDE connection. This script works just fine if the feature class name does not start with an 'S'. If the feature class name does start with an 'S', it gets removed. As you can see in the script below I use Python's lstrip to remove the 'SDE.' from the file name to rename it. The 'SDE.' gets added again automatically after copying. So a feature class named HAM would be renamed SDE.R_HAM_QC. If the feature class is named STREETS it get renamed to SDE.R_TREETS_QC. See how the first S in STREETS is removed? Does anyone have a possible solution?

    #Import modules
    import arcpy, os, sys
    from arcpy import env
    arcpy.env.overwriteOutput = True

    #Set variables
    outWorkspace = "Database Connections\Connection to" 
    arcpy.env.workspace = outWorkspace 
    fileName = "SDE.STREETS"

    #Describe the file name
    desc = arcpy.Describe(fileName)

    if desc.datasetType == "FeatureClass":

          #Rename and copy data if it is a feature class        
          name1 = fileName.lstrip('SDE.')
          print name1
          newName = "R_" + name1 + "_QC"
          outFile = os.path.join(outWorkspace, newName)
          arcpy.Copy_management(fileName, outFile)

What about slicing off the 'SDE.' part instead?

>>> fc = 'SDE.whatever'
>>> fc[4:]
  • That was it! My files are getting renamed properly now. Thank you for the quick response, Chad. – Patty Jula Dec 21 '11 at 16:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.