I can successfully redirect my output to a file, however this appears to overwrite the file's existing data:
import subprocess outfile = open('test','w') #same with "w" or "a" as opening mode outfile.write('Hello') subprocess.Popen('ls',stdout=outfile)
will remove the 'Hello'
line from the file.
I guess a workaround is to store the output elsewhere as a string or something (it won't be too long), and append this manually with outfile.write(thestring)
- but I was wondering if I am missing something within the module that facilitates this.
Popen is more general than subprocess. call . Popen doesn't block, allowing you to interact with the process while it's running, or continue with other things in your Python program. The call to Popen returns a Popen object.
The subprocess module defines one class, Popen and a few wrapper functions that use that class. The constructor for Popen takes arguments to set up the new process so the parent can communicate with it via pipes. It provides all of the functionality of the other modules and functions it replaces, and more.
Popen objects are supported as context managers via the with statement: on exit, standard file descriptors are closed, and the process is waited for.
You sure can append the output of subprocess.Popen to a file, and I make a daily use of it. Here's how I do it: (of course, this is a dummy example, I'm not using subprocess to list files...)
The Popen () process execution can provide some output which can be read with the communicate () method of the created process. from subprocess impor Popen,PIPE p = Popen ( ["ls","-al"],stdout=PIPE) stderr = p.communicate ()
What is Subprocess in Python? Subprocess is the task of executing or running other programs in Python by creating a new process. We can use subprocess when running a code from Github or running a file storing code in any other programming language like C, C++, etc. We can also run those programs that we can run on the command line.
The subprocess.run() function takes various arguments including stdin, stdout and stderr. To redirect the output, you need to pass a file object to stdout.
You sure can append the output of subprocess.Popen
to a file, and I make a daily use of it. Here's how I do it:
log = open('some file.txt', 'a') # so that data written to it will be appended c = subprocess.Popen(['dir', '/p'], stdout=log, stderr=log, shell=True)
(of course, this is a dummy example, I'm not using subprocess
to list files...)
By the way, other objects behaving like file (with write()
method in particular) could replace this log
item, so you can buffer the output, and do whatever you want with it (write to file, display, etc) [but this seems not so easy, see my comment below].
Note: what may be misleading, is the fact that subprocess
, for some reason I don't understand, will write before what you want to write. So, here's the way to use this:
log = open('some file.txt', 'a') log.write('some text, as header of the file\n') log.flush() # <-- here's something not to forget! c = subprocess.Popen(['dir', '/p'], stdout=log, stderr=log, shell=True)
So the hint is: do not forget to flush
the output!
Well the problem is if you want the header to be header, then you need to flush before the rest of the output is written to file :D
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With