Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

append subprocess.Popen output to file?

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.

like image 384
Jdog Avatar asked Sep 12 '11 14:09

Jdog


People also ask

What is difference between subprocess Popen and call?

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.

What is subprocess popen ()?

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.

Is Popen a context manager?

Popen objects are supported as context managers via the with statement: on exit, standard file descriptors are closed, and the process is waited for.

Can I append the output of subprocess to a file?

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...)

How to get the output of the created process In Popen?

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?

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.

How to redirect the output of a subprocess in C++?

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.


2 Answers

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!

like image 95
Joël Avatar answered Oct 10 '22 03:10

Joël


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

like image 35
user3849635 Avatar answered Oct 10 '22 04:10

user3849635