Python Script to Automate a FC to FC/FGDB opertation ArcGIS Pro

1007
23
12-10-2018 10:43 AM
VanceRenforth2
New Contributor III

I am trying to create a script that will automate a Feature Class to Feature Class OR Feature Class to File Geodatabase Conversion. I want to save this in a folder as a back up once a week in case I lose my current data for any reason or need to revert to that download date. So far I have the following code just for the FC to FC operation, and am recieving the error that follows.:

import arcpy
arcpy.env.workspace = "https://services.arcgis.com/T2graiaSQnlmwwmp/arcgis/rest/services/Target_Address_7_6_Backup/FeatureServer"
arcpy.FeatureClassToFeatureClass_conversion("Target Address New",
 "C:\\Users\vmrenfor\Consumers Folders\VSR\BackupExports\VSR Back up\VSR Back up.gdb",
 "Target_Adrress_12_10_2018")

Traceback (most recent call last):
 File "<string>", line 5, in <module>
 File "c:\program files\arcgis\pro\Resources\arcpy\arcpy\conversion.py", line 860, in FeatureClassToFeatureClass
 raise e
 File "c:\program files\arcgis\pro\Resources\arcpy\arcpy\conversion.py", line 857, in FeatureClassToFeatureClass
 retval = convertArcObjectToPythonObject(gp.FeatureClassToFeatureClass_conversion(*gp_fixargs((in_features, out_path, out_name, where_clause, field_mapping, config_keyword), True)))
 File "c:\program files\arcgis\pro\Resources\arcpy\arcpy\geoprocessing\_base.py", line 496, in <lambda>
 return lambda *args: val(*gp_fixargs(args, True))
arcgisscripting.ExecuteError: Failed to execute. Parameters are not valid.
ERROR 000732: Output Location: Dataset C:\Users mrenfor\Consumers Folders\VSR\BackupExports\VSR Back up\VSR Back up.gdb does not exist or is not supported
Failed to execute (FeatureClassToFeatureClass).

Any thoughts on how I can fix this error would be appreciated. I would then like this to be run each subsequent week, naming the file "Target_Address_{Current Date}.

0 Kudos
23 Replies
MichaelVolz
Esteemed Contributor

I would suggest not having spaces in folder names, maybe replace with underscores.  Might not solve your problem, but would be better practice.

JakeSkinner
Esri Esteemed Contributor

Hi Vance,

Looks like you are setting your workspace to a Feature Service.  Are you trying to download a feature service to a local File Geodatabase.  If so, take a look at this tool.

Also, when specifying a path in python you won't want to specify a backslash in the path.  The backslash (\) character is used to escape characters that otherwise have a special meaning, such as newline, backslash itself, or the quote character.  You have a few options to specify a path:

1.  Place an r in front of the path:

r"C:\temp\data.gdb"

2.  Use two backslashes:

"C:\\temp\\data.gdb"

3.  Use forward slashes:

"C:/temp/data.gdb"
JoshuaBixby
MVP Esteemed Contributor

When working with Windows paths, you should use raw string formatting.  You appeared to be escaping the first backslash in your path but none of the others.  In Python "\v" represents the ASCII vertical tab.  Since you didn't escape it, your string has a vertical tab in it, and ArcPy cannot find the path.

VanceRenforth2
New Contributor III

I took your suggestion and add "r" before the path and that worked. 

So now I have:

import arcpy
arcpy.env.workspace = "https://services.arcgis.com/T2graiaSQnlmwwmp/arcgis/rest/services/Target_Address_7_6_Backup/FeatureS..."
arcpy.FeatureClassToFeatureClass_conversion("Target Address New",
r"C:\Users\vmrenfor\Consumers Folders\VSR\BackupExports\VSR Back up\VSR Back up.gdb",
"Target_Adrress_12_10_2018")

BUT, is it possible to change this so that I don't have to specify the date for the out put feature name? So in this example I have 12_10_2018. Is there an option that would automatically change this to the current date? Further is it possible to make this run once a week with out me having to input it?

0 Kudos
JoshuaBixby
MVP Esteemed Contributor
>>> import datetime
>>> 
>>> now = datetime.datetime.now()
>>> "Target_Address_{}".format(now.strftime("%m_%d_%Y"))
'Target_Address_12_12_2018'
>>> 
VanceRenforth2
New Contributor III

So my entire script should look like this?:

import arcpy
import datetime

now = datetime.datetime.now()
arcpy.env.workspace = "https://services.arcgis.com/T2graiaSQnlmwwmp/arcgis/rest/services/Target_Address_7_6_Backup/FeatureServer"
arcpy.FeatureClassToFeatureClass_conversion("Target Address New",
 "C:\\Users\vmrenfor\Consumers Folders\VSR\BackupExports\VSR Back up\VSR Back up.gdb",
 "Target_Address_{}".format(now.strftime("%m_%d_%Y"))
0 Kudos
JoshuaBixby
MVP Esteemed Contributor

I doubt the code will work because you haven't corrected the Windows path issue.  If you correct that, does the run without error and give you what you expect?

0 Kudos
VanceRenforth2
New Contributor III

I apologize that was the old script. Here is what I am currently looking at:

import arcpy
import datetime

now = datetime.datetime.now()
arcpy.env.workspace = "https://services.arcgis.com/T2graiaSQnlmwwmp/arcgis/rest/services/Target_Address_7_6_Backup/FeatureServer"
arcpy.FeatureClassToFeatureClass_conversion("Target Address New",
 r"C:\\Users\vmrenfor\Consumers Folders\VSR\BackupExports\VSR Back up\VSR Back up.gdb",
 "Target_Address_{}".format(now.strftime("%m_%d_%Y"))
0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Does the code run without error?  If it errors, what is the error?  If not, does it give you the results you expect?

0 Kudos