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?

  • 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?. – PolyGeo Feb 5 '18 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. – PolyGeo Feb 6 '18 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/… – Waterman Feb 6 '18 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. – Waterman Feb 6 '18 at 0:21
  • 1
    For future reference: Python FAQ - Why can’t raw strings end with a backslash? – user2856 Feb 6 '18 at 2:52
4

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.

  • 1
    I see - try either of the above options, updated. – AlecZ Feb 6 '18 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. – Bjorn Feb 6 '18 at 0:28
  • 1
    String concatenation using + is troubling enough, but path construction should always be done with os.path.join. – Tom Feb 6 '18 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)). – PolyGeo Feb 6 '18 at 0:59
  • 1
    @Tom I agree - it just depends on where you fall on the ArcPy to Python developer spectrum. – PolyGeo Feb 7 '18 at 20:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.