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).
<SPAN class="comment token"># Import system modules</SPAN>
<SPAN class="keyword token">import</SPAN> arcpy
<SPAN class="comment token"># Set local variables</SPAN>
inTable <SPAN class="operator token">=</SPAN> <SPAN class="string token">"Zarza data"</SPAN>
fieldName <SPAN class="operator token">=</SPAN> <SPAN class="string token">"DipDir"</SPAN>
expression <SPAN class="operator token">=</SPAN> <SPAN class="string token">"Strike( !Shape!, !FID! )"</SPAN>
codeblock <SPAN class="operator token">=</SPAN><SPAN class="string token">""</SPAN>"
open<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"D:/Alex/someplace/ZarzaModule.py"</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">'r'</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token">#opens text file with def for function Strike()</SPAN>
val <SPAN class="operator token">=</SPAN> f<SPAN class="punctuation token">.</SPAN>read<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN>val<SPAN class="punctuation token">)</SPAN>
<SPAN class="string token">""</SPAN>"
arcpy<SPAN class="punctuation token">.</SPAN>CalculateField_management<SPAN class="punctuation token">(</SPAN>inTable<SPAN class="punctuation token">,</SPAN> fieldName<SPAN class="punctuation token">,</SPAN> expression<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"PYTHON_9.3"</SPAN><SPAN class="punctuation token">,</SPAN> codeblock<SPAN class="punctuation token">)</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
I might also need to operate the code outside of my codeblock and have:
codeblock <SPAN class="operator token">=</SPAN> str<SPAN class="punctuation token">(</SPAN>val<SPAN class="punctuation token">)</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN></SPAN>
But I need to fix referencing my opened file first.