How to pass variables between scripts?

Marcelo Ramos mramos at montevideo.com.uy
Sat May 13 00:34:45 CEST 2006


Gross, Dorit (SDRN) escribió:
> Dear list,
>
> I am an absolute newbie to python and would appreciate your help very much
> :)
>
> I am trying to write a little python script that wraps a set of external
> scripts. The external scripts are either also written in python or are simple
> bash scripts. My wrapping script should be able to send an argument to the
> first script, to execute it, to read its results and to send them then as
> arguments into the next script.
>
> This is the first part that I have written so far: 
>
> #! /usr/local/bin/python
> # test_exec.py
>
> import os, sys, glob
>
> fileList = glob.glob('/data/*.ZIP')
>
> for f in fileList:
> 	try: 
> 		globvars = {'infile' : f}
> 		locvars = {}
> 		execfile('/scripts/second.py', globvars(), locvars)
> 	except IOError:
> 		exit(0)
> 	print locvars
>
>
> And this is what happens when calling test_exec.py
>
>  ./test_exec.py 
> Traceback (most recent call last):
>   File "./test_exec.py", line 19, in ?
>     execfile('/scripts/second.py', vars(), results)
> TypeError: 'dict' object is not callable
>
>
> I tried already to modify the script in different ways but wasn't successful
> so far to make it running. Could you maybe help what I am missing? Further, I
> am not sure how the second python has to look like to actually read what is
> given in "globvars" and to sent its results into "locvars". 
>
> Or might os.popen* be a better option?  
>
>
>   

You are calling the dictionary globvars as a function then the error. 
The fixed line is:

execfile('/scripts/second.py', globvars, locvars)



What you want is the function globals().
Try putting this line in second.py:

print globals()['infile']

Using the dictionary returned by globals() you can make second.py to 
read the contents of testexec.py's globvars dictionary.
locvars is populated with the local variables of second.py and that is 
what you want.


Regards.

-- 
Marcelo Ramos
Fedora Core 5 | 2.6.16
Socio UYLUG Nro 125




More information about the Python-list mailing list