user3123767
user3123767

Reputation: 1165

Showing or leaving python error messages when running them by batch

To simplify, I have batch file which runs multiple python programs:

start "01" /wait "C:\python27\python.exe" test1.py
start "02" /wait "C:\python27\python.exe" test2.py

But I found that even if test1.py is not run because of its error, it simply moves on to run test2.py.

It even just closes the window for test1.py as soon as it confronts that error, and just creates another window for test2.py

Of course, if I run test1.py separately by running

python test1.py

then it prints all error messages.

Since I have tens of python files in one batch, it becomes very hard to know which one of these caused the error, and I can't even know what's that error because I can't see the error messages.

How can I make it stop (but not closes the window) when it meets some error, and shows me the error message?

Upvotes: 2

Views: 6037

Answers (1)

Mofi
Mofi

Reputation: 49086

I do not know much about Python. But according to the question it outputs messages to stdout and stderr which only console applications do.

But if python.exe is indeed a console application and not a Windows (GUI) application, it is not really necessary to use start "title" /wait as this just results in calling console application python.exe in a separate command line interpreter process which is the reason why there is no output of python.exe displayed in command line interpreter process in which is executed the batch file.

I suggest to simply try:

@echo off
echo Calling Python with script test1.py.
"C:\python27\python.exe" test1.py
if errorlevel 1 pause
echo Calling Python with script test2.py.
"C:\python27\python.exe" test2.py
if errorlevel 1 pause

For error handling, see for example:

Please use the search feature of Stack Overflow explained on help page How do I search? and also WWW search engines. You surely do nothing which was not already done by other Python users as well and therefore also asked already most likely very often.

We expect that questioners try to find the solution by themselves and not asking others for making their job, see help page What topics can I ask about here?

Upvotes: 2

Related Questions