AnsweredAssumed Answered

How to open another shape file and pass it as a string into the current script

Question asked by alexander.c.audet@maine.edu on Jan 24, 2018
Latest reply on Jan 24, 2018 by rvburton

So I hopefully have a really simple question here. I am writing a codeblock for the CalculateField_management function but due to the length of my codeblock, and the fact that I will have to repeatedly edit it in the future, I want to put the codeblock into another py text file and then open it within a main script and pass it in as a string for CalculateField_management to interpret.

 

This sounds very simple, but when trying to google it, I only ever get help on modules and arguments. The problem with creating a module is that I cannot have my codeblock function defined outside of CalculateField, so the input definition needs to go into the codeblock as a string. This is what I tried so far but it does not know that f is suppose to reference my opened file.

 

File "<string>", line 3, in <module> NameError: name 'f' is not defined  Failed to execute (CalculateField). 

 

# Import system modules
import arcpy

# Set local variables
inTable = "Zarza data"
fieldName = "DipDir"
expression = "Strike( !Shape!, !FID! )"

codeblock ="""
open("D:/Alex/someplace/ZarzaModule.py",'r') #opens text file with def for function Strike()
val = f.read()
print(val)
"""

arcpy.CalculateField_management(inTable, fieldName, expression, "PYTHON_9.3", codeblock)

 

I might also need to operate the code outside of my codeblock and have:  

codeblock = str(val)

But I need to fix referencing my opened file first. 

Outcomes