I have a feature class within a file geodatabase.
I am using the "Iterate row selection" iterator in Model Builder to read each row in the FC. I am then using the "Get Field Value" tool to read the following 3 fields from each row:
"Status"
"User"
"Tile"
I then want to use the "Calculate Value" tool to return a string, which I will later use as a file path to copy a specific file. The logic is as follows: If "Status" is "Available", return C:\Data\ + "Tile" otherwise return C:\Data\ + "Tile" + _ + "User"
In the Calculate Value tool, I've put the following into the expression field:
a("%Status%", "%User%", "%Tile%")
I've put the following into the Code Block:
def a(Status, User, Tile):
if Status == "Available":
return "C:\Data\" + Tile
else:
return "C:\Data\" + "Tile" + "_" + User
I have the "Data type" set as string.
My model is as follows:
enter image description here
I get the following error for my code block:
ERROR 000989
Python syntax error: Parsing error SyntaxError: EOL while scanning string literal (line 3)
I'm happy to have a solution using model builder or using a stand along python script.
2 Answers 2
You should escape the \
character, this way:
def a(Status, User, Tile):
if Status == "Available":
return "C:\\Data\\" + Tile
else:
return "C:\\Data\\" + "Tile" + "_" + User
-
yeah, or other options: 1. r"C:\data\" + Tile 2. r"C:\"Data"\{0}".format(Tile)mr.adam– mr.adam2015年02月02日 16:27:57 +00:00Commented Feb 2, 2015 at 16:27
-
1@mr.adam
r'c:\data\'
is still a syntax error. Raw strings that end with a backslash still need the final backslash to be represented as `\`Jason Scheirer– Jason Scheirer2015年02月02日 16:43:35 +00:00Commented Feb 2, 2015 at 16:43 -
@JasonScheirer ah, thanks for pointing that out. Double slashes would be good here then.mr.adam– mr.adam2015年02月02日 16:47:44 +00:00Commented Feb 2, 2015 at 16:47
The problem, as indicated in gcarillo's answer, are the single \
characters, specially before the closing quote.
Some solutions:
- Use
/
instead of\
. Windows normally uses backslashes, but forward slashes work too except in the command prompt (cmd.exe). - Raw strings can be useful, but they don't help with strings that end in a backslash.
- My favorite solution in cases like this: use os.path.join() instead of hardcoded path delimiters.
Example using os.path.join():
import os.path
def a(Status, User, Title):
if Status == "Available":
return os.path.join("C:", "Data", Tile)
else:
return os.path.join("C:", "Data", "Tile_" + User) # Or did you mean os.path.join("C:", "Data", Tile + "_" + User) ?
Explore related questions
See similar questions with these tags.