user3123767
user3123767

Reputation: 1165

Sequentially running multiple python programs "with sys" using batch

I am using Windows based PC.

I have multiple python programs with "sys". I mean the programs have these lines.

import sys
input_file = sys.argv[1]
output_file = sys.argv[2]

So I run these programs by running

python program1.py input1 output1

However, there are series of python programs so it will be convenient if I can run all these by double-clicking only one file.

How can I run them all sequentially, not simultaneously?

I tried things like

start C:\python27\python.exe program1.py input1 output1
start C:\python27\python.exe program2.py input2 output2

But it did not work..

Upvotes: 3

Views: 9025

Answers (2)

Richard
Richard

Reputation: 47

import os

os.system('python' + ' ' + 'python_1.py')

os.system('python' + ' ' + 'python_2.py')

This solution is itself a python program. You can also construct the program names and have this in a loop. You can also add a parameter after the program name.

Upvotes: 0

Mofi
Mofi

Reputation: 49086

Call is mainly for calling a batch file from within a batch file or running a block in current batch file as subroutine.

Start is the command to start any application as a separate process which means for a console applications to run it in a separate command prompt (console) window. A GUI application executed from within a batch file is always started as a separate process even if command start is not used.

Running an application results in halting the execution of the batch file until the application terminates itself for most applications. (There are applications with a different behavior caused by the application itself.) But a console or GUI application started using command start results in immediate continuation of batch file execution.

With using start /wait ... it is possible to start a console or GUI application as a separate process and halt execution of the batch file until the application terminates itself.

start /wait C:\python27\python.exe program1.py input1 output1
start /wait C:\python27\python.exe program2.py input2 output2

On running start /? in a command prompt window, the help for this command is printed into the output window.

["title"] means that optionally a title can be set for the new command prompt window (used only on starting a console application). I mention this here because command start can interpret any string in double quotes anywhere on the command line also as window title. Therefore if the application to start or one of its parameters must be enclosed in double quotes because of a space character or one of these characters &()[]{}^=;!'+,`~ in path or file name, it is better to explicitly specify a title string in double quotes immediately after command start as first parameter which can be even an empty string like "" (best for GUI applications).

start "Python Task 1" /wait "C:\python27\python.exe" program1.py input1 output1
start "Python Task 2" /wait "C:\python27\python.exe" program2.py input2 output2

Upvotes: 5

Related Questions