Questions tagged [popen]

popen() is a way to communicate with subprocesses using a file-like interface. It originated in C, but has been ported to other languages (via extensions) such as Python.

FILE *popen(const char *command, const char *type)

popen() (Process OPEN) is a method by which programs can start and communicate with other programs using a file-like interface. This funcion is not mandated by ANSI, but is specified by POSIX.

popen() allows the programmer to avoid the internal workings of fork() and pipe() (this is how it is implemented on UNIX-like systems) by presenting a file object. This allows for the use of functions such as fprintf() and fscanf(), presenting a more orthogonal interface to the programmer (excepting closing the process - the programmer must use pclose() to stop a subprocess).

This function has been ported to a number of programming languages, such as Python (os.popen), Ruby (IO.popen), Tcl (open |command), etc.

2441 questions
29
votes
1 answer

Python subprocess Popen.communicate() equivalent to Popen.stdout.read()?

Very specific question (I hope): What are the differences between the following three codes? (I expect it to be only that the first does not wait for the child process to be finished, while the second and third ones do. But I need to be sure this is…
Christophe
  • 517
  • 2
  • 5
  • 9
28
votes
4 answers

Subprocess.Popen: cloning stdout and stderr both to terminal and variables

Is it possible to modify code below to have printout from 'stdout 'and 'stderr': printed on the terminal (in real time), and finally stored in outs and errs variables? The code: #!/usr/bin/python3 # -*- coding: utf-8 -*- import subprocess def…
Łukasz Zdun
  • 283
  • 1
  • 3
  • 6
27
votes
6 answers

Running a process in pythonw with Popen without a console

I have a program with a GUI that runs an external program through a Popen call: p = subprocess.Popen("" , stdout=subprocess.PIPE , stderr=subprocess.PIPE , cwd=os.getcwd()) p.communicate() But a console pops up, regardless of what I do…
sbirch
  • 871
  • 1
  • 10
  • 18
26
votes
7 answers

How to get environment from a subprocess?

I want to call a process via a python program, however, this process need some specific environment variables that are set by another process. How can I get the first process environment variables to pass them to the second? This is what the program…
anonymous
26
votes
1 answer

How to do multiple arguments with Python Popen?

I am trying to make a PyGtk Gui, that has a button. When the user presses this button, gnome-terminal prompts the user to write their password. Then it will clone this Git repository for gedit JQuery snippets. And then, it copies the js.xml file to…
Voidcode
  • 1,229
  • 1
  • 18
  • 22
25
votes
2 answers

Intercepting stdout of a subprocess while it is running

If this is my subprocess: import time, sys for i in range(200): sys.stdout.write( 'reading %i\n'%i ) time.sleep(.02) And this is the script controlling and modifying the output of the subprocess: import subprocess, time, sys print…
Paul
  • 42,322
  • 15
  • 106
  • 123
25
votes
1 answer

how to pass "one" argument and use it twice in "xargs" command

I tried to use the xargs to pass the arguments to the echo: [usr@linux scripts]$ echo {0..4} | xargs -n 1 echo 0 1 2 3 4 the -n 1 insured that the xargs pass 1 arguments a time to the echo. Then I want to use this aruments twice, however the…
spring cc
  • 937
  • 1
  • 10
  • 19
25
votes
6 answers

Can popen() make bidirectional pipes like pipe() + fork()?

I'm implementing piping on a simulated file system in C++ (with mostly C). It needs to run commands in the host shell but perform the piping itself on the simulated file system. I could achieve this with the pipe(), fork(), and system() system…
Taylor D. Edmiston
  • 12,088
  • 6
  • 56
  • 76
25
votes
8 answers

real time subprocess.Popen via stdout and PIPE

I am trying to grab stdout from a subprocess.Popen call and although I am achieving this easily by doing: cmd = subprocess.Popen('ls -l', shell=True, stdout=PIPE) for line in cmd.stdout.readlines(): print line I would like to grab stdout in…
alfredodeza
  • 5,058
  • 4
  • 35
  • 44
25
votes
3 answers

How to capture the exit_code and stderr of the command that is run in C++?

I'm writing a c++ program that executes and outputs (in real-time) a shell script, makefile or just another program. However I would like to have my program return differently when there are errors or no error. #include "execxi.h" using namespace…
Logan
  • 10,649
  • 13
  • 41
  • 54
25
votes
3 answers

Why does subprocess.Popen() with shell=True work differently on Linux vs Windows?

When using subprocess.Popen(args, shell=True) to run "gcc --version" (just as an example), on Windows we get this: >>> from subprocess import Popen >>> Popen(['gcc', '--version'], shell=True) gcc (GCC) 3.4.5 (mingw-vista special r3) ... So it's…
Ben Hoyt
  • 10,694
  • 5
  • 60
  • 84
24
votes
4 answers

subprocess.wait() not waiting for Popen process to finish (when using threads)?

I am experiencing some problems when using subprocess.Popen() to spawn several instances of the same application from my python script using threads to have them running simultaneously. In each thread I run the application using the popen() call,…
iceaway
  • 1,204
  • 1
  • 8
  • 12
23
votes
8 answers

kill a process started with popen

After opening a pipe to a process with popen, is there a way to kill the process that has been started? (Using pclose is not what I want because that will wait for the process to finish, but I need to kill it.)
Sophie Alpert
  • 139,698
  • 36
  • 220
  • 238
23
votes
7 answers

Python, Popen and select - waiting for a process to terminate or a timeout

I run a subprocess using: p = subprocess.Popen("subprocess", stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE) This subprocess could either exit…
George Yuan
22
votes
5 answers

Python - How to call bash commands with pipe?

I can run this normally on the command line in Linux: $ tar c my_dir | md5sum But when I try to call it with Python I get an error: >>> subprocess.Popen(['tar','-c','my_dir','|','md5sum'],shell=True) >>> tar:…
Greg
  • 45,306
  • 89
  • 231
  • 297