Move SHP to gdb and ArcView license (newbie frustration)!!!

3144
54
04-11-2012 07:58 AM
LucaMoiana
New Contributor
Hi there,
What I need to do is move all the files into an mxd to a newly created gdb, chechking prj, and update mxd reference.

I have ArcGIS 10 SP4, license ArcView

I studied a bit of Python and get familiar with basic, then I copied some written code and pasted all together, attached is the code.

My frustration is that i can???t even move my shp files to a created gdb, is that because of my ArcView license?

PLEASE PLEASE PLEASE HELP

# 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)
Tags (2)
0 Kudos
54 Replies
LucaMoiana
New Contributor
This code works:

# 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")


If I use this: arcpy.CopyFeatures_management(fc, outWorkspace)
I don't get any error but the gdb is empty.
If I add:
# 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:
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 13, in <module>
    arcpy.Delete_management("c:\\park\\test4.gdb")
  File "C:\Program Files\ArcGIS\Desktop10.0\arcpy\arcpy\management.py", line 3031, in Delete
    raise e
ExecuteError: ERROR 000601: Cannot delete c:\park\test4.gdb.  May be locked by another application.

Not that I care about CopyFeatures_managment but it seems to be fast, when I sue it out of this script obviously
0 Kudos
MichaelVolz
Esteemed Contributor
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.
0 Kudos
LucaMoiana
New Contributor
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:

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.
0 Kudos
MichaelVolz
Esteemed Contributor
Luca:

In the line arcpy.CopyFeatures_management(fc, outWorkspace) can you change fc to outFeatureClass

Also can you move the statement print os.path.join(workspace, fc, "TO GDB") above

the statement arcpy.CopyFeatures_management(fc, outWorkspace) as this is where the error is occurring.
0 Kudos
LucaMoiana
New Contributor
Done, and here is the error I get:

>>> 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).
0 Kudos
MichaelVolz
Esteemed Contributor
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.
0 Kudos
LucaMoiana
New Contributor
Sorry, as said I am a total newbie. I got what you say but, what should I put there if not the gdb name?


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.
0 Kudos
LucaMoiana
New Contributor
Sorry, as said I am a total newbie. I got what you say but, what should I put there if not the gdb name?

Here is the link to the example I used...
http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//001700000035000000
0 Kudos
MichaelVolz
Esteemed Contributor
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.
0 Kudos
LucaMoiana
New Contributor
That's embarassing, I had some Python training (via webcourse) but apparently ain't enough...
This is my side project, to get new skills.
I did as suggested and it works fine for a while, then I get the following error (SHP are the ones I used to test the precious scripts):

>>> 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.
0 Kudos