Hi,
Â
Â
you are defining the variable "txt" inside a function block, which means that the variable will only exist inside the function block (this is called the scope of the variable). Once the function block is terminated, the variable "txt" is destroyed (it goes out of scope).
Â
Â
You can, however, force your variable to be globally accessible using the "
global" reserved word in Python. Your variable will then have a global scope, meaning it will exist for the entire duration of your Python instance. Example:
Â
Â
def processFeature(feature):
Â
  global txt   txt = feature.getAttribute('text_log')   pass
Â
You can now access the variable "txt" from other Python blocks within the same workspace instance, e.g. other PythonCallers, shutdown scripts, etc.
Â
Â
David
Â
Â
Ok thank's for your answer, but I'm still stuck,Â
Â
Â
How can I access to this variable outside the block :
Â
Â
def processFeature(feature):   global txt   txt = feature.getAttribute('text_log')   txt = "lpplp"   pass  .. Something here is missing ...]  log.write(txt)  o... this doesn't work ]
Â
Â
The string is the result of this function, When I call it by using "string = processFeature()" -> error, similarly "string = processFeature(feature)" -> again error.Â
Â
Â
What must I specify within the () ?Â
Â
Â
thank's
Â
Â
Â
Â
Hi,
Â
Â
I have some questions. I guess that "log" refers to a file object. Is it right? If so, where and how did you open the file? And when do you need to write "txt" into the file? Need to write "text_log" of every feature when the PythonCaller receives it?
Â
Â
Takashi
Well here is the entire code :Â
Â
Â
----
Â
path = "C:\\\\Python27\\\\Codes\\\\new\\\\"    log = open(''+path+'log.txt', 'r+') old = log.read() log.seek(0)  def processFeature(feature):   global txt   txt = feature.getAttribute('text_log')   pass  log.write(txt) log.write("\\n" +old) log.close()Â
Â
---
Â
Â
What I try to do is to open a txt file, write the value of an attribute (text_log in that case)Â and close it.
Â
Â
Any clue ?
FME calls function or class method which is specified to "Class or Function to Process Features" parameter of the PythonCaller. Codes which are written in the global scope in the PythonCaller script usually will not be executed. A workaround would be: 1) Open the file in Startup Python Script. 2) Write "txt" and close the file in Shutdown Python Script.
Thank's Takashi,
Â
I'm gonna try this, but I'm wondering if there is not an easiest way to do that. What I want is to write information in ONE text file. My workbench is pretty big and I want to write down intermediary results during the running. However, it is not possible to duplicate text file writer, and targetting every intermediate result into one writer is not feasible (at least, not human readable...) Â
Â
Does anyone know how to do that ?
Â
Â
CU
Â
Regis
Just as an example. If you opened a file in Startup Script like this: ---- # Startup Python Script g_log = open('C:\\\\FME\\\\tmp\\\\log.txt', 'w') ----  since "g_log" is a global variable, it can be accessed from anywhere in the same workspace instance. For example, a PythonCaller with this script writes "text_log" for every input feature into the file. ----- # PythonCaller Script import fmeobjects def processFeature(feature):   g_log.write('%s\\n' % feature.getAttribute('text_log')) -----
Forgot closing the file. ----- # Shutdown Python Script g_log.close() ----- Â In addition, if you need to write "text_log" into the same file at multiple locations in the workspace, you can define a common function in Startup Script, like this. ----- # Startup Python Script Example import fmeobjects g_log = open('C:/FME/tmp/log.txt', 'w') def logText(feature): Â Â g_log.write('%s\\n' % feature.getAttribute('text_log')) ----- In that case, it's not necessary to write script for each PythonCaller. Just specify the function name ("logText" in the example above) to "Class or Function to Process Features" of every PythonCaller.
Â
Â
Most appropriate way depends on your target and conditions.