Hello, I am relatively new to Python scripting, and trying to automate a task at work. Basically, the script is supposed to copy a shape file [named 'backbone'] from the env.workspace into the specified file GDB. The script works fine, but I am trying to run this script numerous times by changing the env.workspace and re-running the script.
With that being said, if there was a feature class in the file GDB already named 'backbone', the original script would copy the new one over the original. I thought that arcpy.ValidateName() was the function used to prevent this from happening, and it would rename the new feature class so the original would not be written over; is this a misconception on my end?
After some trial and error, I was able to add a nested if statement that checked for a feature class named 'backbone', and if true, would add a '1' to the end of the new feature class's name. Here lies the problem: if I change the env.workspace to another file location, it will just restart my counter variable and overwrite 'backbone1'. I'm sure the solution is simple, just beyond my elementary knowledge base; any help would be greatly appreciated! I have copy and pasted the script below:
import arcpy
from arcpy import env
env.overwriteOutput = True
env.workspace = 'C:/Users/MWright/Downloads/YaleToSC/'
outputEnv = 'M:/Drafting/IECcoop/PhaseIII/PawneeToYale/PawneeToYales.gdb/'
x = 'backbone'
count = 1
fclist = arcpy.ListFeatureClasses()
print(fclist)
print('\n')
for fc in fclist:
fcdesc = arcpy.Describe(fc)
fcname = fcdesc.basename
if fcname == 'backbone':
if arcpy.Exists(x):
fcout = arcpy.ValidateTableName(fcname, outputEnv)
arcpy.CopyFeatures_management(fc, outputEnv + fcout + str(x))
print('\n' + fcname + ' has been copied to GDB.\n')
x += 1
else:
fcout = arcpy.ValidateTableName(fcname, outputEnv)
arcpy.CopyFeatures_management(fc, outputEnv + fcout + str(count))
else:
print(fcname + " feature class does not need to be copied to geodatabase.")
print('\nBackbone feature class copy process complete.')