1

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?

user3123767
  • 1,115
  • 3
  • 13
  • 22
  • Have you tried exception handling? https://wiki.python.org/moin/HandlingExceptions – Ashoka Lella Jul 11 '14 at 10:23
  • You could also make some loggers to put all the error messages into a text file. https://docs.python.org/2/howto/logging – moarCoffee Jul 11 '14 at 10:39
  • @moarCoffee Hmm.. how should I change my batch code to get the log file of all python programs that the batch file ran? When I run it, I see windows like https://drive.google.com/file/d/0B1sEqo7wNB1-TXZSZXR3SVBoS1E/edit?usp=sharing – user3123767 Jul 12 '14 at 03:25

1 Answers1

2

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?

Community
  • 1
  • 1
Mofi
  • 46,139
  • 17
  • 80
  • 143
  • Thank you, Mofi. test1.py is run, "Press any key to continue" message appears, I press any key, test2.py is run, Press any key to continue message appears, the program ends. It feels like this is appoaching to the right direction. But it asks "Press any key to continue" even if the program didn't have any error. I hope it just goes on. All I want is to see the messages while running multiple python programs sequentially. – user3123767 Jul 12 '14 at 03:19
  • Well, I suggested only a small code to verify if `Python.exe` is indeed a console application and therefore command `start` is not necessary at all. I updated the code to pause execution only if **Python** terminates with a return value greater or equal 1. – Mofi Jul 12 '14 at 13:45