subprocess - Python Popen: Write to stdout AND log file simultaneously -


I am using popen to call a shell script which is consistently typing in a stdout and stderr in the log file is. Is there any way to continuously output the log file (on screen), or alternatively, write the shell script in both log file and stdout at the same time?

I basically want to do something like this in Python:

  cat file 2> And 1. Tee -a logfile # "cat file" will be replaced with some scripts   

Again, this pipe stderr / stdout simultaneously t, which it writes both for stdout and my logfile

I know how to write stdout and stderr in a logfile in Python. Where I'm stuck is how to duplicate it back to the screen:

  Subprocess. Pope ("cat file", shell = true, stdout = logfile, stderr = logfile)   

Of course I could do just that, but would it be any T and Shell file descriptor redirection Is there any way to do without ?:

  Subprocess. Poppen ("cat file 2 & gt; and amp; 1 | tee -a logfile", shell = true)    

You can use the pipe to read data from the program's standout and write it in all the places you want to:

  import system import subprocess logfile = open ('Logfile', 'w') process = Subprocess.Popen (['cat', 'file'], stdout = subprocess.ipip, stderr = subprocess.STDOUT) for line in proc.stdout: sys.stdout.write (Line) logfile.write (line) in proc dragon 3,  universal_newlines  The parameter controls how the pipe is used (wait)   

UPDATE

If incorrect , The pipe reads byte objects and may need to be decoded (e.g., line line.decode ('utf-8') ) Get a string if true , the dragon decodes for you

has been changed to version 3.3: when the universal_newswidth is true, then the client locals If using encoding locale does Hakgetrofredendekoding (false) .getpreferredencoding (). For more information on this change see the IO.TextIowrapper category.

Comments

Popular posts from this blog

excel vba - How to delete Solver(SOLVER.XLAM) code -

github - Teamcity & Git - PR merge builds - anyway to get HEAD commit hash? -

ios - Replace text in UITextView run slowly -