Good day.
I would need an advice about sys.stdout and sys.stderr.
I use Python 2.6, on Windows XP SP3, but i think the question is the same for UNIX/ MAC computers.
I tried all possible combinations before posting this message, so ANY advice would be great.
I work with "subprocess" module. I call an application, and i want to save 3 things : the return code, the string that application prints on the screen and possibly the error message, BUT i also want to see the message LIVE, while the application prints it.
I get exactly the 3 things i need with this call :
import subprocess
p=subprocess.Popen( 'C:\\PROGRA~1\\7-Zip\\7z.exe a -t7z "C:/archive.7z" "C:/boot.ini" -m0=LZMA -ms=on -mmt=on -mx9', stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
ret = p.communicate()
I must log all 3 messages with logging module. I need to process the message that my application prints on the screen.
I used 7-Zip to compress C:\boot.ini, just as an example.
The return code is "p.returncode". The string that application prints on the screen is "ret[0]", the error message is "ret[1]". It's everything i need...
It's also okay to use os.system( "program" + " arguments" ), but in this case i think storing the message is hard to do.
The ONLY problem is that if i make my 7-Zip compress large files, the execution stops for a veeery long time ... and i get all the messages imediately AFTER the program closes. This is bad, because i don't know where i am, how much longer do i have to wait and if there is a problem, i can only see it after a very long time...
I don't mind if i save the return code and the messages AFTER the program closes. I want both : to store all 3 variables AND see the program print its messages live, line by line - not after the program exits.
So i tried all kinds of methods, with a cStringIO file as pipe, with a temporary file in memory, i even tried to create a hook class to re-write sys.stdout, so that all messages that print on the screen are also recorded in a list, than retrieve the list and log all output... I an sure i am doing it completely wrong !
I think it must be a simple thing to do.
I'm sorry i am not advanced in C/ C++ to hack the implementation of the core functions in Python. I don't know much about sys.stdout, sys.stderr, print >>, or buffers.
Any help is apreciated.
Thank you.