1

I'm using ArcGIS Desktop 10.2.2.

I'm getting the following error when I run a script in Arc.

SyntaxError: EOL while scanning string literal

The error message points to the line where I set my workspace variable "wspace".

Here are the relevant portions of my code:

# Set the local variables
wspace = r"Q:\GW\EC1210WQAEH_QESEA\CSSP_ATL\Users\vanvulpenc\CSV_to_Shapefile\temp_workspace"
x_coords = "Lng"
y_coords = "Lat"
output_Layer = "output_Layer"
saved_Layer = "To_Shapefile.lyr"

Then later in my script the one line that references the "wspace" variable is:

arcpy.CopyFeatures_management(wspace+saved_Layer, Output_FC)

Is there a better way to format my path perhaps?

asked Feb 5, 2018 at 23:38
9
  • There is a lot of code here that is not related to the specific question that you are asking. Please review Writing code snippets to get quicker answers?. Commented Feb 5, 2018 at 23:55
  • That is not the line that is throwing the error. Wasn't it coming from CopyFeatures? You need just a few more lines for a suitable code snippet in this instance. Commented Feb 6, 2018 at 0:09
  • @PolyGeo - that was the error in my original question: gis.stackexchange.com/questions/270057/… Next, following your advice, I ran it from the Python window as per this question: gis.stackexchange.com/questions/270392/… Commented Feb 6, 2018 at 0:15
  • @PolyGeo - Next, I noticed the original error message showed I lacked a backslash between my "wspace" and my "saved_Layer" so that's why only one line of code here. With this question all I'm trying to do is zero in on the one line of code that's currently being flagged in the error message. All the rest of my code can be found in the first question. Commented Feb 6, 2018 at 0:21
  • 1
    For future reference: Python FAQ - Why can’t raw strings end with a backslash? Commented Feb 6, 2018 at 2:52

1 Answer 1

5

You have everything set up correctly. Simply take the '\' character off the end of the variable workspace:

wspace = r"Q:\GW\EC1210WQAEH_QESEA\CSSP_ATL\Users\vanvulpenc\CSV_to_Shapefile\temp_workspace"

Python will see \" as an escape character - for reference, if you wanted to include that backslash at the end of the string (which you don't want to here), you would end the line with \\".

Now, if you need to join this with a filename later on - you can do this one of two ways. First, you can join them like this:

output = wspace + '\\' + layer

Note the double backslash, then call the output variable in your arcpy call. Better still, import os at the beginning of your script, and use os.path.join inside your arcpy call:

os.path.join(wspace, layer)

That will eliminate any errors.

answered Feb 6, 2018 at 0:04
9
  • 1
    I see - try either of the above options, updated. Commented Feb 6, 2018 at 0:27
  • 2
    I agree with @AlecZ on getting rid of the final backslash. Using os.path.join instead of merging two path strings is better because it will make sure the merge is done correctly. Commented Feb 6, 2018 at 0:28
  • 1
    String concatenation using + is troubling enough, but path construction should always be done with os.path.join. Commented Feb 6, 2018 at 0:55
  • 1
    I agree with @Tom that os.path.join is a good way to construct paths but I usually prefer to use Python string formatting like r"{0}\{1}".format(wspace,Output_FC)). Commented Feb 6, 2018 at 0:59
  • 1
    @Tom I agree - it just depends on where you fall on the ArcPy to Python developer spectrum. Commented Feb 7, 2018 at 20:14

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.