0

I need to execute two python script script1.py (path: dir1) and script2.py(path:dir2) in loop.In order to run this two script I need to give the python path. Earlier I used to manually set path and execute the script. Since I need to execute script in loop, How can i create a batch file that could execute one script and after its work is done execute another. I am newbie with batch script. Thanks

asked Oct 8, 2015 at 12:26
1
  • What operating system or shell are you using and what do you mean by "need to give the python path"? Commented Oct 8, 2015 at 12:55

2 Answers 2

0

You can use a python script that calle the subprocess.call() method. It takes a list of strings containing the commands to be called.
You then use that with a try loop to check if the first script executes without errors, and, if so, call the next (optionally with a try as well).

#batch.py
import subprocess
 try:
 subprocess.call(['python3','path/to/script1.py'])
 except Exception as e:
 print('Error: ', e) #alternatively add your logger here
 sys.exit(1)
 try:
 subprocess.call(['python3','path/to/script2.py'])
 except Exception as e:
 print('Error: ', e) #alternatively add your logger here
 sys.exit(1)

you would then execute this using python3 path/to/batch.py from the CLI.

Take note that, if you're using python 2.7 (which you shouldnt unless absolutely necessary), you have to alter the code to call python instead of python3.

For more references and information on the subprocess module check out the python documentation and this answer

answered Oct 8, 2015 at 12:43
Sign up to request clarification or add additional context in comments.

Comments

0

You can create a shell script (which runs for 10 times) for this:

#!/bin/bash
for i in `seq 0 10` ;
do
 echo "Running Script 1"
 python Script1.py <path as argument>;
 echo "script 1 completed"
 echo "Running Script 2"
 python Script2.py <path as argument>;
 echo "script 2 completed"
done

Please clarify if your need is something else.

martineau
124k29 gold badges181 silver badges319 bronze badges
answered Oct 8, 2015 at 12:38

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.