arcpy.CopyFeatures_management syntax?

3091
12
08-22-2016 07:17 AM
VincentLaunstorfer
Occasional Contributor III

Hi,

In a python script, I use arcpy.CopyFeatures_management function but it certain situation, some parameters may have a white space such as:

C:\ESRI\City Points_point.shp

Then, when running the script, arcpy.CopyFeatures_management interprets C:\ESRI\City Points_point.shp as 2 parameters because of the white space.

Executing: CopyFeatures 'C:\ESRI\City Points_point.shp' C:\ESRI\City_Points_point.shp # 0 0 0Start Time: Mon Aug 22 16:01:33 2016Failed to execute. Parameters are not valid.

How would it be possible to arrange the syntax and have 'C:\ESRI\City Points_point.shp' interpreted as just one parameter?

Thanks

Tags (2)
0 Kudos
12 Replies
DanPatterson_Retired
MVP Emeritus

preference is to fix the file name to remove spaces...

alternate is raw formatting which may or may not work in all cases

current "C:\ESRI\City Points_point.shp"

raw   r"C:\ESRI\City Points_point.shp"   r in front

0 Kudos
VincentLaunstorfer
Occasional Contributor III

Dan,

I understand what you mean... Indeed, the purpose of my script is to replace the white space in shapefiles contained in a workspace:

# Import arcpy module
import arcpy
import os

# Split input values using a semicolon.
inputs = sys.argv[1]
inputlist = inputs.split(";")

#Character to be replaced
old = sys.argv[2]

#New character
new = sys.argv[3]

outWorkspace = sys.argv[4]

try:
    # Loop through the list of inputs.
    for input in inputlist:

        arcpy.AddMessage("Input: "  + input)

        newname = input.replace(old, new)

        arcpy.AddMessage("Newname: "  + newname)
        
        #VALIDATE THE NEW FEATURE CLASS NAME FOR THE OUTPUT WORKSPACE
        output = outWorkspace + os.sep + newname[newname.rfind(os.sep)+1:]

        arcpy.AddMessage("Output: "  + output)
        
        #MESSAGE
        arcpy.AddMessage("Converting " + str(input[input.rfind(os.sep)+1:]) + " to " + str(output[output.rfind(os.sep)+1:]))
          
        #COPY FEATURES
        arcpy.CopyFeatures_management(input,output, "", "0", "0", "0")

        arcpy.Delete_management(input)
        arcpy.AddMessage(str(input[input.rfind(os.sep)+1:]) + " Deleted")


        arcpy.AddMessage("Processed" + "\n")              

except:
    arcpy.AddMessage(arcpy.GetMessages())

But it crashed when filemanes have white spaces!

Any guess?

0 Kudos
DanPatterson_Retired
MVP Emeritus

you should be using the Rename tool see the code snippet

0 Kudos
VincentLaunstorfer
Occasional Contributor III

Dan,

I inserted the code for the Rename tool instead of copy tool in my script, as below.

But I still get an error:

Executing: ReplacingSHPBATCH 'C:\ESRI\City Points_point.shp' " " _ C:\ESRI
Start Time: Tue Aug 23 12:44:00 2016
Running script ReplacingSHPBATCH...
Input: 'C:\ESRI\City Points_point.shp'
Output: 'C:\ESRI\City_Points_point.shp'
Converting City Points_point.shp' to City_Points_point.shp'
Executing: Rename 'C:\ESRI\City Points_point.shp' 'C:\ESRI\City_Points_point.shp' ShapeFile
Start Time: Tue Aug 23 12:44:00 2016
Failed to execute. Parameters are not valid.
ERROR 000732: Input Data Element: Dataset 'C:\ESRI\City Points_point.shp' does not exist or is not supported
Failed to execute (Rename).
Failed at Tue Aug 23 12:44:00 2016 (Elapsed Time: 0.01 seconds)
Completed script ReplacingSHPBATCH...
Succeeded at Tue Aug 23 12:44:00 2016 (Elapsed Time: 0.06 seconds)

Why?

# Import arcpy module
import arcpy
from arcpy import env
import os

# Split input values using a semicolon.
inputs = sys.argv[1]
inputlist = inputs.split(";")

#Character to be replaced
old = sys.argv[2]

#New character
new = sys.argv[3]

# Set workspace
env.workspace = sys.argv[4]

try:
    # Loop through the list of inputs.
    for input in inputlist:

        arcpy.AddMessage("Input: "  + input)
        output = input.replace(old, new)

        arcpy.AddMessage("Output: "  + output)
        
        #MESSAGE
        #arcpy.AddMessage("Converting " + str(input[input.rfind(os.sep)+1:]) + " to " + str(output[output.rfind(os.sep)+1:]))
          
        #COPY FEATURES
        #arcpy.CopyFeatures_management(input,output, "", "0", "0", "0")

        # Execute Rename
        arcpy.Rename_management(input, output, "ShapeFile")
        #arcpy.Delete_management(input)
        #arcpy.AddMessage(str(input[input.rfind(os.sep)+1:]) + " Deleted")

        arcpy.AddMessage("Processed" + "\n")              

except:
    arcpy.AddMessage(arcpy.GetMessages())

0 Kudos
DanPatterson_Retired
MVP Emeritus

Well it is going to be difficult since you have spaces in the paths, but you are obviously batching this and you need raw format to overcome the spaces... try a manual filename instead of sys.argv[1] with raw format and see if it works before further debugging

VincentLaunstorfer
Occasional Contributor III

That's better!

If I put the filename instead of sys.argv[1], it's works. It means the bug comes from the sys.argv[1]... The script cannot deal with the arguments properly!

input = "City Points_point.shp"

Any guess?

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

I don't think the issue is having a space in the name, at least in terms of the geoprocessing tools themselves.  As much as I don't use spaces in geodata set names I create, I come across data with spaces in the names, and I have never had the Copy Features or Rename tool fail because a space existed.  As Dan mentioned, you will want to try raw string formatting to make sure the escape characters are being handled correctly.

0 Kudos
VincentLaunstorfer
Occasional Contributor III

In this case, how can I use raw formatting in the syntax above with a variable?

r"input"? It does not work...

What is the correct syntax?

Thanks

0 Kudos
DanPatterson_Retired
MVP Emeritus

well, apparently you are running this as a tool, so perhaps a list of the tool parameters will be useful.  Featureclass for sys.argv[1] I presume? And since it is multivalue, a list of how you handled all the others as well would be useful.  I had assumed you were running this as a script

0 Kudos