could anyone help me regarding calling different python scripts from within a main script.How can this be acheived?

Thanks in advance

Recommended Answers

All 3 Replies

There are a few ways:

(1) import os
os.system("external_script.py")

(2) import os
out = os.popen("external_script.py")
output = out.readlines() # can get output of script

(3) import the script if the external script contains function or class
that you want to use.


Practice Python Online
http://pyschools.com

Thanks for the reply..the first method works great, but what if I want to handle the exceptions in the main script that is not handled in called script??

i.e., if I call script.py from main.py, and I want to handle unhandled exceptions of script.py in main.py. What can be done?

There are a few ways:

(1) import os
os.system("external_script.py")

(2) import os
out = os.popen("external_script.py")
output = out.readlines() # can get output of script

(3) import the script if the external script contains function or class
that you want to use.


Practice Python Online
http://pyschools.com

In your external script, if it runs smoothly, you may return 'sys.exit(0)'.
Else if there is any exception, you can try to catch it and return 'sys.exit(1)'

Then in your main script, you may do the following:

if os.system('external_script')>>8:
   print 'Error running external script'

Practice Python Online
http://pyschools.com

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.