# Import system modules import arcpy from arcpy import env import os # Set environment settings env.workspace = "C:/Users/a391665/Documents/AAAAPERSONALE/rugareto" # Execute CreateFileGDB arcpy.CreateFileGDB_management("C:/park", "test4") # Set local variables outWorkspace = "c:/park/test4.gdb" # Use ListFeatureClasses to generate a list of shapefiles in the # workspace shown above. fcList = arcpy.ListFeatureClasses() # Execute CopyFeatures for each input shapefile for shapefile in fcList: # Determine the new output feature class path and name outFeatureClass = os.path.join(outWorkspace, shapefile.strip(".shp")) arcpy.CopyFeatures_management(shapefile, outFeatureClass)
# Import system modules import arcpy from arcpy import env import os # Set environment settings env.workspace = "C:\\Users\\a391665\\Documents\\AAAAPERSONALE\\rugareto" print("env.workspace completed successfully") #check GDB exist if os.path.exists("c:\\park\\test4.gdb"): arcpy.Delete_management("c:\\park\\test4.gdb") # Execute CreateFileGDB arcpy.CreateFileGDB_management("C:\\park", "test4") print("CreateFileGDB completed successfully") # Set local variables outWorkspace = "c:\\park\\test4.gdb" print("outWorkspace completed successfully") # Iterate def fcs_in_workspace(workspace): arcpy.env.workspace = workspace for fc in arcpy.ListFeatureClasses(): print os.path.join(workspace, fc) arcpy.FeatureClassToGeodatabase_conversion(fc, outWorkspace) print os.path.join(workspace, fc, "TO GDB") for ws in arcpy.ListWorkspaces(): fcs_in_workspace(os.path.join(workspace, ws)) fcs_in_workspace("C:\\Users\\a391665\\Documents\\AAAAPERSONALE\\rugareto")
# Iterate def fcs_in_workspace(workspace): arcpy.env.workspace = workspace for fc in arcpy.ListFeatureClasses(): print os.path.join(workspace, fc) outFeatureClass = os.path.join(outWorkspace, fc.strip(".shp")) arcpy.CopyFeatures_management(fc, outWorkspace) print os.path.join(workspace, fc, "TO GDB") for ws in arcpy.ListWorkspaces(): fcs_in_workspace(os.path.join(workspace, ws)) fcs_in_workspace("C:\\Users\\a391665\\Documents\\AAAAPERSONALE\\rugareto")
# Import system modules import arcpy from arcpy import env import os # Set environment settings env.workspace = "C:\\Users\\a391665\\Documents\\AAAAPERSONALE\\rugareto" print("env.workspace completed successfully") #check GDB exist if os.path.exists("c:\\park\\test4.gdb"): arcpy.Delete_management("c:\\park\\test4.gdb") # Execute CreateFileGDB arcpy.CreateFileGDB_management("C:\\park", "test4") print("CreateFileGDB completed successfully") # Set local variables outWorkspace = "c:\\park\\test4.gdb" print("outWorkspace completed successfully") # Iterate def fcs_in_workspace(workspace): arcpy.env.workspace = workspace for fc in arcpy.ListFeatureClasses(): print os.path.join(workspace, fc) outFeatureClass = os.path.join(outWorkspace, fc.strip(".shp")) arcpy.CopyFeatures_management(fc, outWorkspace) print os.path.join(workspace, fc, "TO GDB") for ws in arcpy.ListWorkspaces(): fcs_in_workspace(os.path.join(workspace, ws)) fcs_in_workspace("C:\\Users\\a391665\\Documents\\AAAAPERSONALE\\rugareto")
Luca:
Can you provide the feedback you get for both scenarios from the print statements?
Also, for the code where you get ERROR 000601, can you make sure you do not have the file geodatabase that you are trying to delete open in ArcMap or ArcCatalog on your computer as that might be putting a lock on the file geodatabase. You also might want to close out of the application that you are running python through as well (e.g. IDLE or pythonwin) as that might also have a lock on the file geodatabase if you had an error in the middle of running the script.
>>> env.workspace completed successfully
CreateFileGDB completed successfully
outWorkspace completed successfully
C:\Users\a391665\Documents\AAAAPERSONALE\rugareto\Percorsi.shp
C:\Users\a391665\Documents\AAAAPERSONALE\rugareto\Percorsi.shp\TO GDB
Traceback (most recent call last):
File "C:\Python26\ArcGIS10.0\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", line 312, in RunScript
exec codeObject in __main__.__dict__
File "C:\Users\a391665\Desktop\Script2_subfolders_test.py", line 34, in <module>
fcs_in_workspace("C:\\Users\\a391665\\Documents\\AAAAPERSONALE\\rugareto")
File "C:\Users\a391665\Desktop\Script2_subfolders_test.py", line 30, in fcs_in_workspace
arcpy.CopyFeatures_management(outFeatureClass, outWorkspace)
File "C:\Program Files\ArcGIS\Desktop10.0\arcpy\arcpy\management.py", line 1943, in CopyFeatures
raise e
ExecuteError: Failed to execute. Parameters are not valid.
ERROR 000732: Input Features: Dataset c:\park\test4.gdb\Percorsi does not exist or is not supported
ERROR 000725: Output Feature Class: Dataset c:\park\test4.shp already exists.
Failed to execute (CopyFeatures).
Ok, deleted gbd, closed alla application, except for PythonWin.
As said, if I only add " arcpy.CopyFeatures_management(fc, outWorkspace)" it works smoothly but the gdb is empty.
If I run this code:# Import system modules import arcpy from arcpy import env import os # Set environment settings env.workspace = "C:\\Users\\a391665\\Documents\\AAAAPERSONALE\\rugareto" print("env.workspace completed successfully") #check GDB exist if os.path.exists("c:\\park\\test4.gdb"): arcpy.Delete_management("c:\\park\\test4.gdb") # Execute CreateFileGDB arcpy.CreateFileGDB_management("C:\\park", "test4") print("CreateFileGDB completed successfully") # Set local variables outWorkspace = "c:\\park\\test4.gdb" print("outWorkspace completed successfully") # Iterate def fcs_in_workspace(workspace): arcpy.env.workspace = workspace for fc in arcpy.ListFeatureClasses(): print os.path.join(workspace, fc) outFeatureClass = os.path.join(outWorkspace, fc.strip(".shp")) arcpy.CopyFeatures_management(fc, outWorkspace) print os.path.join(workspace, fc, "TO GDB") for ws in arcpy.ListWorkspaces(): fcs_in_workspace(os.path.join(workspace, ws)) fcs_in_workspace("C:\\Users\\a391665\\Documents\\AAAAPERSONALE\\rugareto")
I get this error:
>>> env.workspace completed successfully
CreateFileGDB completed successfully
outWorkspace completed successfully
C:\Users\a391665\Documents\AAAAPERSONALE\rugareto\Percorsi.shp
Traceback (most recent call last):
File "C:\Python26\ArcGIS10.0\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", line 312, in RunScript
exec codeObject in __main__.__dict__
File "C:\Users\a391665\Desktop\Script2_subfolders_test.py", line 34, in <module>
fcs_in_workspace("C:\\Users\\a391665\\Documents\\AAAAPERSONALE\\rugareto")
File "C:\Users\a391665\Desktop\Script2_subfolders_test.py", line 29, in fcs_in_workspace
arcpy.CopyFeatures_management(fc, outWorkspace)
File "C:\Program Files\ArcGIS\Desktop10.0\arcpy\arcpy\management.py", line 1943, in CopyFeatures
raise e
ExecuteError: Failed to execute. Parameters are not valid.
ERROR 000725: Output Feature Class: Dataset c:\park\test4.shp already exists.
Failed to execute (CopyFeatures).
Luca:
In the ESRI documentation for CopyFeatures_management, the first required parameter is the input feature which looks correct. The second required parameter is the output feature class. It looks like you are providing a workspace instead of the name of the output feature class.
Sorry, as said I am a total newbie. I got what you say but, what should I put there if not the gdb name?
>>> env.workspace completed successfully
CreateFileGDB completed successfully
outWorkspace completed successfully
C:\Users\a391665\Documents\AAAAPERSONALE\rugareto\Percorsi.shp
C:\Users\a391665\Documents\AAAAPERSONALE\rugareto\Percorsi.shp\TO GDB
C:\Users\a391665\Documents\AAAAPERSONALE\rugareto\test.shp
C:\Users\a391665\Documents\AAAAPERSONALE\rugareto\test.shp\TO GDB
C:\Users\a391665\Documents\AAAAPERSONALE\rugareto\test2.shp
C:\Users\a391665\Documents\AAAAPERSONALE\rugareto\test2.shp\TO GDB
C:\Users\a391665\Documents\AAAAPERSONALE\rugareto\DataForArcPad1\clip_agricole_comuni.shp
C:\Users\a391665\Documents\AAAAPERSONALE\rugareto\DataForArcPad1\clip_agricole_comuni.shp\TO GDB
C:\Users\a391665\Documents\AAAAPERSONALE\rugareto\DataForArcPad1\clip_boschi_comuni.shp
C:\Users\a391665\Documents\AAAAPERSONALE\rugareto\DataForArcPad1\clip_boschi_comuni.shp\TO GDB
C:\Users\a391665\Documents\AAAAPERSONALE\rugareto\DataForArcPad1\clip_boschi_comuni_ptishp_pti.shp
C:\Users\a391665\Documents\AAAAPERSONALE\rugareto\DataForArcPad1\clip_boschi_comuni_ptishp_pti.shp\TO GDB
C:\Users\a391665\Documents\AAAAPERSONALE\rugareto\DataForArcPad1\clip_boschi_comuni_ptishp_pti2.shp
C:\Users\a391665\Documents\AAAAPERSONALE\rugareto\DataForArcPad1\clip_boschi_comuni_ptishp_pti2.shp\TO GDB
C:\Users\a391665\Documents\AAAAPERSONALE\rugareto\DataForArcPad1\clip_boschi_lat_comuni.shp
C:\Users\a391665\Documents\AAAAPERSONALE\rugareto\DataForArcPad1\clip_boschi_lat_comuni.shp\TO GDB
C:\Users\a391665\Documents\AAAAPERSONALE\rugareto\DataForArcPad1\clip_boschi_lat_comuni_Proje.shp
C:\Users\a391665\Documents\AAAAPERSONALE\rugareto\DataForArcPad1\clip_boschi_lat_comuni_Proje.shp\TO GDB
C:\Users\a391665\Documents\AAAAPERSONALE\rugareto\DataForArcPad1\clip_dusaf_comuni.shp
C:\Users\a391665\Documents\AAAAPERSONALE\rugareto\DataForArcPad1\clip_dusaf_comuni.shp\TO GDB
C:\Users\a391665\Documents\AAAAPERSONALE\rugareto\DataForArcPad1\clip_urb_comuni.shp
C:\Users\a391665\Documents\AAAAPERSONALE\rugareto\DataForArcPad1\clip_urb_comuni.shp\TO GDB
C:\Users\a391665\Documents\AAAAPERSONALE\rugareto\DataForArcPad1\clip_urb_comuni_Project.shp
C:\Users\a391665\Documents\AAAAPERSONALE\rugareto\DataForArcPad1\clip_urb_comuni_Project.shp\TO GDB
C:\Users\a391665\Documents\AAAAPERSONALE\rugareto\DataForArcPad1\rugareto.gdb\whitewash_mask
C:\Users\a391665\Documents\AAAAPERSONALE\rugareto\DataForArcPad1\rugareto.gdb\whitewash_mask\TO GDB
C:\Users\a391665\Documents\AAAAPERSONALE\rugareto\DataForArcPad1\rugareto.gdb\Comuni_rugareto
C:\Users\a391665\Documents\AAAAPERSONALE\rugareto\DataForArcPad1\rugareto.gdb\Comuni_rugareto\TO GDB
C:\Users\a391665\Documents\AAAAPERSONALE\rugareto\DataForArcPad1\rugareto.gdb\Rugareto
C:\Users\a391665\Documents\AAAAPERSONALE\rugareto\DataForArcPad1\rugareto.gdb\Rugareto\TO GDB
C:\Users\a391665\Documents\AAAAPERSONALE\rugareto\DataForArcPad1\rugareto.gdb\Buffer_confine
C:\Users\a391665\Documents\AAAAPERSONALE\rugareto\DataForArcPad1\rugareto.gdb\Buffer_confine\TO GDB
C:\Users\a391665\Documents\AAAAPERSONALE\rugareto\DataForArcPad1\rugareto.gdb\Rugareto_Buffer
C:\Users\a391665\Documents\AAAAPERSONALE\rugareto\DataForArcPad1\rugareto.gdb\Rugareto_Buffer\TO GDB
C:\Users\a391665\Documents\AAAAPERSONALE\rugareto\GIS\Maratona\ARANCIO.shp
C:\Users\a391665\Documents\AAAAPERSONALE\rugareto\GIS\Maratona\ARANCIO.shp\TO GDB
C:\Users\a391665\Documents\AAAAPERSONALE\rugareto\GIS\Maratona\BLU.shp
C:\Users\a391665\Documents\AAAAPERSONALE\rugareto\GIS\Maratona\BLU.shp\TO GDB
C:\Users\a391665\Documents\AAAAPERSONALE\rugareto\GIS\Maratona\ROSSO.shp
C:\Users\a391665\Documents\AAAAPERSONALE\rugareto\GIS\Maratona\ROSSO.shp\TO GDB
C:\Users\a391665\Documents\AAAAPERSONALE\rugareto\GIS\Maratona\RUGARETO.shp
Traceback (most recent call last):
File "C:\Python26\ArcGIS10.0\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", line 312, in RunScript
exec codeObject in __main__.__dict__
File "C:\Users\a391665\Desktop\Script2_subfolders_test.py", line 34, in <module>
fcs_in_workspace("C:\\Users\\a391665\\Documents\\AAAAPERSONALE\\rugareto")
File "C:\Users\a391665\Desktop\Script2_subfolders_test.py", line 32, in fcs_in_workspace
fcs_in_workspace(os.path.join(workspace, ws))
File "C:\Users\a391665\Desktop\Script2_subfolders_test.py", line 32, in fcs_in_workspace
fcs_in_workspace(os.path.join(workspace, ws))
File "C:\Users\a391665\Desktop\Script2_subfolders_test.py", line 29, in fcs_in_workspace
arcpy.CopyFeatures_management(fc, outWorkspace + "\\" + fc.strip(".shp"))
File "C:\Program Files\ArcGIS\Desktop10.0\arcpy\arcpy\management.py", line 1943, in CopyFeatures
raise e
ExecuteError: Failed to execute. Parameters are not valid.
ERROR 000725: Output Feature Class: Dataset c:\park\test4.gdb\RUGARETO already exists.
Failed to execute (CopyFeatures).
Try putting in outWorkspace + "\\" + fc.strip(".shp"). You might have issues with "\ as an escape character so you might want to change your code to use / instead of \ as this is the full path to the output feature class including its name.
I would also strongly advise getting some ESRI training on python scripting if your manager allows this. You would speed up your python scripting learning curve tremendously.