1

I am trying to add a file path field to a feature layer in an ArcPy notebook in ArcGIS Pro. I have a variable with the beginning of the path (the path to the folder) and an existing field with the file name. I have been looking at other questions on here and on ArcGIS help websites (e.g. How to use variable in expression in arcpy CalculateField tool?) but haven't found a way that works. If we pretend the folder is just my downloads folder, here is what I have tried as expressions within the arcpy.management.CalculateField command:

output_folder = "C:/Users/ellah/Downloads/"

expression = 'output_folder + !match.Filename!'
expression = str(output_folder) + '!match.Filename!'
expression = " ".join([str(i) for i in [output_folder, !match.Filename!] if i])
expression = "{}/{}".format(output_folder, !match.Filename!)
expression = "{0} + str(!match.Filename!)".format(output_folder)

... as well as some other small tweaks. I've tried using them as the expression as well as defining them as a function in the code block and running them that way. Everything works fine if I type out the actual folder path ('C:/Users/ellah/Downloads/') rather than calling the variable. I don't think it's an issue with the forward/backslashes or typos like a double slash.

Some errors I have gotten include:

ERROR 000539: Traceback (most recent call last): File "", line 1, in NameError: name 'output_folder' is not defined

ERROR 000539: File "", line 1 C:/Users/ellah/Downloads/out/u"1_in.png" ^ SyntaxError: invalid syntax

6
  • 1
    Have you tried import os, os.path.join(output_folder,'!match.Filename!')? Commented Dec 5, 2023 at 15:57
  • @aldo_tapia That certainly seems helpful. Are you imagining it within the expression or code block in arcpy.management.CalculateField, or separately? Commented Dec 5, 2023 at 16:14
  • expression needs to end up as a string containing valid python code that can be parsed standalone. Try expression = "'{}' + '!match.Filename!'".format(output_folder) Commented Dec 6, 2023 at 0:25
  • @mikewatt Thank you for the help! That expression returns the correct complete path, but with some extra character surrounding the match.Filename part (e.g., C:/Users/ellah/Downloads/outu"3_in.png" where it should be C:/Users/ellah/Downloads/out/3_in.png). Do you know how to remove the u and the ""s? Commented Dec 7, 2023 at 17:08
  • If output_folder = "C:/Users/ellah/Downloads/" then set the expression = "'{0}'!match.Filename!".format(output_folder) you are getting the u" because you are wrapping the !match.Filename! in ' ' instead of using the pure value from the !match.Filename! field. Commented Dec 7, 2023 at 18:24

1 Answer 1

1

Here's an example

enter image description here

Running the code below with !filename! field within the ' ' in the expression.

arcpy.management.CalculateField('in_features', 'filepath', "'{0}!filename!'".format(output_folder))

enter image description here

look at the difference in the expression !filename! field is outside of ' '

arcpy.management.CalculateField('in_features', 'filepath', "'{0}'!filename!".format(output_folder))

enter image description here

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.