[Python-au] Calling two Python Scripts from another script PartII

Duane Hennessy duaneh at connexus.net.au
Sun Nov 23 00:10:26 CET 2003


Another way to run python scripts from another script is if the scripts 
you want to run are just a consecutive list of commands without a 'main' 
function or anything then you can more easily execute them like the 
following example...

'Script1.py' has the following commands
    print 'Hello from Script1'

To run this I write the following

### code starts here ###

file = open(r'c:\temp\Script1.py','r')
exec(file.read())
file.close()

### code ends here ###

Result:
    Hello from Script1

Easy as that! For giggles I ran the following test which is fun too, I 
used three scripts all executing each other.

### Script1.py Code ###
print 'Hello from Script1'
f = open(r'c:\temp\Script2.py','r')
exec(f.read())
f.close()
### end code ###

### Script2.py Code ###
print 'Hello from Script2'
f = open(r'c:\temp\Script3.py','r')
exec(f.read())
f.close()
### end code ###

### Script3.py Code ###
print 'Hello from Script3'
### end code ###

Result:
    Hello from Script1
    Hello from Script2
    Hello from Script3




More information about the python-au mailing list