outFeatureClass/arcpy.CopyFeatures_management

4148
13
12-29-2015 04:47 AM
CliveSwan
Occasional Contributor II

I need to automate a shapefile to feature class update. The problem is that the shapefile name and output (feature class) are not the same.

So I need to hard code the shp & features.

I have a loop that works, printing out the shapefile names.

The code fails when I try to use the arcpy.CopyFeatures_management function.

Can anyone see what is causing the arcpy.CopyFeatures_management function to fail??

Would appreciate a suggestion to resolve the issue.

import arcpy
import glob
import os
import sys
import csv
import time
import smtplib
import shutil
import ftplib


sdeConnection = arcpy.env.workspace = r"C:\database_connections\GIST.sde"
outWorkspace =sdeConnection

datadir = arcpy.env.workspace = r"\\server1\ER Shares\Highway_Layer_Update" ## URL works

try:
    #check file exists here
    fcList = arcpy.ListFeatureClasses()

    ## Feature classes are hard coded
    ## Feature classes are hard coded
    for i in fcList:
        if i == "Bus Stops.shp":
            #outFeatureClass = os.path.join(outWorkspace, shapefile.strip(".shp"))
            #arcpy.CopyFeatures_management(shapefile, outFeatureClass)
            print i  ## NO shp
            fcnew = "GISADMIN.KCC_BUSSTOPS_NEW"
            outFeatureClass = os.path.join(outWorkspace, fcnew )
            arcpy.CopyFeatures_management(i,  outFeatureClass)
            arcpy.DeleteFeatures_management(i)
        elif i == "Cluster_sites_2014.shp":
            print i            ## Prints shp
            fcnew = "GISADMIN.KCC_KHS_CLUSTERS_2014_NEW"
            outFeatureClass = os.path.join(outWorkspace, fcnew )  ## Fails
            printoutFeatureClass
            arcpy.CopyFeatures_management(i, outFeatureClass)
            arcpy.DeleteFeatures_management(i)
        else:
            print "No file"
except:
    print ("No File found program terminated")  ## This is printed out

Message was edited by: Dan Patterson I formatted your code to make it easier to read... use the syntax highlighting ... >> ... and select Python

0 Kudos
13 Replies
RebeccaStrauch__GISP
MVP Emeritus

back to your original question....first, I would recommend commenting out the

     arcpy.DeleteFeatures_management(i)

line until you have it running.

Have you tried it without including the GISADMIN owner in the name?  I know different ArcGIS versions have switched back and forth as to whether it wants it or it doesn't.  I've had script that ran perfectly in one version, only to have to modify it to allow for the opposite situation in a different version.  Running it manually once, then copying the python snippet from the results tab might help you see the format it wants in your version.

Also, I'm assuming you have many than just these two?  If not, I would just do it manually and be done with it. If you have many, rather than having a whole string of   If == statements, maybe make an array with the old and new names   [[old1, new1], [old2, new2], []...]   and loop thru that, or if you are just trying to remove the spaces in the name, you can do some string manipulation on the .shp name from your listfeatures to automate the new name.

Just some ideas.

0 Kudos
CliveSwan
Occasional Contributor II

I amended the code, the error message indicated there was an existing Feature class.

The arcpy.copymanagement works, the arcpy.deletemanagement still fails??

sdeConnection = arcpy.env.workspace = r"C:\database_connections\GIST.sde"

outWorkspace =sdeConnection

datadir = arcpy.env.workspace = r"\\server1\\GISData\Highway_Layer_Update"

##print datadir

try:

    ##check file exists here

    fcList = arcpy.ListFeatureClasses()

    for i in fcList:

        if i == "Bus Stops.shp":     ### no file

            #outFeatureClass = os.path.join(outWorkspace, shapefile.strip(".shp"))

            #arcpy.CopyFeatures_management(shapefile, outFeatureClass)

            print i

            fcnewbus = "GISADMIN.BUSSTOPS_NEW"

            outbus = os.path.join(outWorkspace, fcnewbus )

            arcpy.CopyFeatures_management(i,  outbus)

           arcpy.DeleteFeatures_management(r"\\server1\\GISData\Highway_Layer_Update\Bus Stops.shp")

##            #arcpy.DeleteFeatures_management(datadir + "\Bus Stops.shp")

        elif i == "Cluster_sites.shp":  ## file exsists

            print i

            fcnewcluster = "GISADMIN.CLUSTERS_NEW"

            outcluster = os.path.join(outWorkspace, fcnewcluster)

            arcpy.CopyFeatures_management(i, outcluster)   ## WORKS!!!

          arcpy.DeleteFeatures_management(r"r"\\server1\\GISData\Highway_Layer_Update\Cluster_sites.shp")

           ## DELETE FAILS

        

            ##arcpy.DeleteFeatures_management(datadir + "\Cluster_sites_2014.shp")

Error message existing connexion forcibly closed????

MessageFile NameLinePosition
Traceback
    __call__C:\Program Files (x86)\PyScripter\Lib\rpyc.zip\rpyc\core\netref.py196
    syncreqC:\Program Files (x86)\PyScripter\Lib\rpyc.zip\rpyc\core\netref.py71
    sync_requestC:\Program Files (x86)\PyScripter\Lib\rpyc.zip\rpyc\core\protocol.py431
    serveC:\Program Files (x86)\PyScripter\Lib\rpyc.zip\rpyc\core\protocol.py379
    _recvC:\Program Files (x86)\PyScripter\Lib\rpyc.zip\rpyc\core\protocol.py337
    recvC:\Program Files (x86)\PyScripter\Lib\rpyc.zip\rpyc\core\channel.py50
    readC:\Program Files (x86)\PyScripter\Lib\rpyc.zip\rpyc\core\stream.py166
exceptions.EOFError: [Errno 10054] An existing connection was forcibly closed by the remote host
0 Kudos
DanPatterson_Retired
MVP Emeritus

SDE stuff... sorry, check your script logic on a local file if you can before you try to debug scripts loading from other connections.

0 Kudos
RebeccaStrauch__GISP
MVP Emeritus

you may want to use the "Advanced Editor" in the upper right corner to edit your last reply (must have it open in a new Tab...not in the geonet inbox).

It's have to tell is

##      #arcpy.DeleteFeatures_management(datadir + "\Bus Stops.shp")

is an indentation error or not.    But my copy/paste was weird, so maybe might be the issue.

I would use

filetodelete = arcpy.os.path.join(datadir, "Bus Stops.shp")
arcpy.DeleteFeatures_Management(filetodelete)

or

arcpy.DeleteFeatures_management(datadir + "\\Bus Stops.shp")

...notice double \\

... also, you have a double    r"r"   in

arcpy.DeleteFeatures_management(r"r"\\server1\\GISData\Highway_Layer_Update\Cluster_sites.shp")