2

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.

PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked Feb 2, 2015 at 16:21

2 Answers 2

4

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
answered Feb 2, 2015 at 16:25
3
1

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) ?
answered Feb 2, 2015 at 23:28

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.