run method, use an additional argument named captureoutputTrue.
What's wrong with my new code? Please let me know how to fix this.ĭesired Output in both console and log.txt file 1. How do I get output to run from subprocess To capture the output of the subprocess. I was hoping to get similar output on both console. I was hoping to get similar output on both console and log.txt file. Some of the output only displayed on the console (os.system('ver') and n('whoami')) while the print() function was only displayed on log.txt file and not in the console anymore. Some of the output only displayed on the console ( os.system('ver') and n('whoami')) while the print() function was only displayed on log.txt file and not in the console anymore. Unfortunately, this didn't really work as expected.
Windows 7: command executed gives the output & return code is 0 Windows 8: command executed gives the output & return code is 255 Both Windows 7 and 8 gives me same expected output. After') # This appear in message.log only, but NOT in console I use python subprocess module on windows 7 & 8 machines Found return codes are different when compared. import subprocess, time from threading import Thread def enqueueoutput(out): print 'Hello from enqueueoutput' for line in iter(out. n('whoami') # This appear in console only, but NOT in message.log Os.system('ver') # This appear in console only, but NOT in message.log Before") # This appear in message.log only, but NOT in console Then, I decided to have a copy on a log file (log.txt) too while maintaining the original output to the console. Python: Getting live output from subprocess using poll Using subprocess.Popen, subprocess.call, or subprocess.checkoutput will all invoke a process using Python, but if you want live output coming from stdout you need use subprocess.Popen in tandem with the Popen.poll method. It gets more complicated if you want to monitor several subprocesses in parallel. A problem occurs when you need to process the output of a long-running process as soon as it is emitted: the output is usually buffered. Below showing the example for running command 'dir findstr py', the trick here is using stdin and stdout params to chain them up. Initial Code to display output in the console only import os, subprocess It is very easy to run a sub-process in python using its subprocess module. call ( 'dir', shell True) Run under Windows.
Initially, I've a simple program to print out the whole output to the console.