-2

Suppose I have three Python script named script1.py, script2.py, script3.py I want to write another python script named Allscript.py which contain the all the above 3 script name along with their path. Note that all the three scripts are in different location. Let path of script1 is at C:\abc\script1.py, script2 is at C:\pqr\script2.py, script3 is at C:\xyz\script3.py

My requirement is I want to run the script one after another, first it will run script1, then after successful completion of script1 it will run script2 and so on.

I find the below script from stackoverflow. I want something like that. Please in Answer provide me that script which can execute all the 3 python script.

import os
print "Starting script1"
os.system("python script1.py arg1 arg2 arg3")
print "script1 ended"
print "Starting script2"
os.system("python script2.py arg1 arg2 arg3")
print "script2 ended"
print "Starting script3"
os.system("python script3.py arg1 arg2 arg3")
print "script3 ended"
Chanda Korat
  • 2,453
  • 2
  • 19
  • 23
Aquil Abbas
  • 167
  • 2
  • 11
  • What's wrong with that code? – Daniel Roseman Feb 27 '17 at 12:16
  • This code I get from the question which is used to run multiple codes concurrently. Which is not my requirement – Aquil Abbas Feb 27 '17 at 12:19
  • But it doesn't do that; there is nothing concurrent here. – Daniel Roseman Feb 27 '17 at 12:22
  • I am new to this language sir, You guidance is really appreciated. So the above code run the mentioned script one after another? – Aquil Abbas Feb 27 '17 at 12:25
  • The thing is Sir, All the 3 scripts are in different file location, and i don't want to disturb the path of all these files. So to call all the files where should i mention the file path, as the above code doesn't contain the paths of the file. – Aquil Abbas Feb 27 '17 at 12:27
  • @AquilAbbas, there is nothing wrong with this code. All you have to do is define where `script(n).py` is located for the os terminal to execute it. You can either define them before hand if they are very long, or you can directly inject them like @Szabolcs does below. – pstatix Feb 27 '17 at 12:49
  • Thanks for you help sir,, Really appreciating. – Aquil Abbas Feb 27 '17 at 13:23

1 Answers1

2

It just works fine for me.

C:\abc\script1.py:

print 'Executing script1'

C:\pqr\script2.py:

print 'Executing script2'

C:\xyz\script3.py:

print 'Executing script3'

With the modifications you mentioned create the Allscript.py like this:

Allscript.py:

import os
print 'Starting script1'
os.system (r"python C:\abc\script1.py")
print 'Script1 ended'
print 'Starting script2'
os.system (r"python C:\pqr\script2.py")
print 'Script2 ended'
print 'Starting script3'
os.system (r"python C:\xyz\script3.py")
print 'Script3 ended'

Then if you execute it like python Allscript.py it would produce the output:

Starting script1
Executing script 1
Script1 ended
Starting script2
Executing script 2
Script2 ended
Starting script3
Executing script 3
Script3 ended

Hope it helps.

Update for python3 with some exception handling:

script1.py

print("script 1 is running")

script2.py

print("script 2 is running")
exit("something bad happened")

script3.py

print("script 3 is running")

allscript.py

import subprocess


def execute_script(script_path):

    print(f"starting execution of {script_path}")
    try:
        subprocess.run(["python", script_path], check=True)
    except Exception as exc:
        print(f"error during execution of {script_path}: {str(exc)}")
    else:
        print(f"finished execution of {script_path}")


execute_script("/path/to/script1.py")
execute_script("/path/to/script2.py")
execute_script("/path/to/script3.py")

After executing allscript.py the output looks like:

starting execution of /path/to/script1.py
script 1 is running
finished execution of /path/to/script1.py

starting execution of /path/to/script2.py
script 2 is running
something bad happened
error during execution of /path/to/script2.py: Command '['python', '/path/to/script2.py']' returned non-zero exit status 1.

starting execution of /path/to/script3.py
script 3 is running
finished execution of /path/to/script3.py
Szabolcs
  • 3,990
  • 18
  • 38
  • Hi Szabolcs, Suppose while executing AllScript.py one of my internal script like Script1 encountered an error, the aAllScript will skip that and continue, But Now I want to populate the error and also want AllScript to stop executing further. What should I do to solve that. – Aquil Abbas Feb 28 '17 at 03:10
  • Hi Aquil! You could check this question for further reference: http://stackoverflow.com/questions/30664263/return-value-from-one-python-script-to-another – Szabolcs Feb 28 '17 at 11:47
  • Hi Szabolcs, The link you refer is somehow changing scripts. I don't want any minute change in my code. I just want to add exception in the AllScript.py from where other scripts are calling. I want to handle exception in AllScript.py script, If any script fails, AllScript.py exit instead of skipping that python file and executing others remaining. – Aquil Abbas Mar 02 '17 at 14:38
  • @AquilAbbas hey, my use-case is same. did you get the approach? – Bushra Saifi Jul 03 '21 at 16:21