I've encountered an issue implementing a code into ArcMap. The original code was:
for fc in fclist:
... arcpy.Clip_analysis(fc, r’C:\GIS_LabManual\Lab10_Manual\Lab10_data\Arb_clip_poly.shp’, r’C:\GIS_LabManual\Lab10_Manual\Lab10_results\’+fc)
The error it shows is "Parsing error SyntaxError: invalid syntax (line 2)."
I then tried to adjust it to the following code (which also had the same error):
for fc in fclist:
... arcpy.Clip_analysis(fc,r’F:GIS_Joseph\Lab10_Joseph\Data_Joseph\Lab_10_data\Lab_10_data\Arb_clip_poly.shp’,r’F:GIS_Joseph\Lab10_Joseph\Results_Joseph\’+fc)
Could you tell what the error might be?
2 Answers 2
I think your single quote characters around the file paths are causing the error. You begin the string with an end quote and end the string with another end quote. Instead of r’C:\path’
try r'C:\path'
.
Also, make sure you include a slash after the drive letter in your file path, like you did the first time but not the second time: C:\path
as opposed to F:path
.
-
Thank you for the response. However, I tried it and it gave me a different error: "Parsing error SyntaxError: EOL while scanning string literal (line 2)." I made sure I have the slash everywhere such as in the following code: for fc in fclist: ... arcpy.Clip_analysis(fc,r'F:\GIS_Joseph\Lab10_Joseph\Data_Joseph\Lab_10_data\Lab_10_data\Arb_clip_poly.shp',r'F:\GIS_Joseph\Lab10_Joseph\Results_Joseph\'+fc)Daniel Joseph– Daniel Joseph2018年11月13日 23:05:46 +00:00Commented Nov 13, 2018 at 23:05
-
2@DanielJoseph you can't have a single slash before the end quote. Make the very last one a double slash
Results_Joseph\\'+fc)
2018年11月14日 05:54:16 +00:00Commented Nov 14, 2018 at 5:54 -
True, the single backslash is a separate issue. From what I've been taught, you can also solve it by changing backslashes to forward slashes in your code. Either way, I think you need to fix that AS WELL AS the single quote characters like I said previously to eliminate all errors. (I tried using your first file path, which did not end with a single backslash, to define a variable, and the ArcMap Python interpreter didn't recognize it as a string and gave me the syntax error you first reported. When I changed the single quote marks, it did recognize the string.)Matt Leonard– Matt Leonard2018年11月14日 15:27:25 +00:00Commented Nov 14, 2018 at 15:27
Your other issue is that your raw string ends with a backslash. Python doesn't like this.
x=r'c:\'
File "<stdin>", line 1
x=r'c:\'
^
SyntaxError: EOL while scanning string literal
Instead, use os.path.join
to put your file paths together:
import os.path
for fc in fclist:
arcpy.Clip_analysis(fc, r’C:\GIS_LabManual\Lab10_Manual\Lab10_data\Arb_clip_poly.shp’, os.path.join(r’C:\GIS_LabManual\Lab10_Manual\Lab10_results', fc))
...
in your code, or did you put it into the question to help show indent? (your actual code should have a tab or a couple spaces, not...
)