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
47
votes
10 answers

How do I get 'real-time' information back from a subprocess.Popen in python (2.5)

I'd like to use the subprocess module in the following way: create a new process that potentially takes a long time to execute. capture stdout (or stderr, or potentially both, either together or separately) Process data from the subprocess as it…
Ryan
  • 4,179
  • 6
  • 30
  • 31
44
votes
6 answers

Python: subprocess and running a bash script with multiple arguments

How do I go about running a bash script using the subprocess module, to which I must give several arguments? This is what I'm currently using: subprocess.Popen(['/my/file/path/programname.sh', 'arg1 arg2 %s' % arg3], \ shell = True) The bash…
user2510173
  • 601
  • 1
  • 6
  • 9
41
votes
3 answers

subprocess.Popen in different console

I hope this is not a duplicate. I'm trying to use subprocess.Popen() to open a script in a separate console. I've tried setting the shell=True parameter but that didn't do the trick. I use a 32 bit Python 2.7 on a 64 bit Windows 7.
Ionut Hulub
  • 6,180
  • 5
  • 26
  • 55
41
votes
1 answer

Python subprocess and user interaction

I'm working on a GUI front end in Python 2.6 and usually it's fairly simple: you use subprocess.call() or subprocess.Popen() to issue the command and wait for it to finish or react to an error. What do you do if you have a program that stops and…
Dave Brunker
  • 1,559
  • 5
  • 15
  • 23
41
votes
4 answers

How to get output from subprocess.Popen(). proc.stdout.readline() blocks, no data prints out

I want output from execute Test_Pipe.py, I tried following code on Linux but it did not work. Test_Pipe.py import time while True : print "Someting ..." time.sleep(.1) Caller.py import subprocess as subp import time proc =…
wearetherock
  • 3,721
  • 8
  • 36
  • 47
38
votes
2 answers

Python subprocess.Popen() wait for completion

I am writing a small script to serially walk through a directory and run a command on the subdirectories therein. I am running into a problem however with Popen() that it will walk through the directories and run the desired command without waiting…
DJMcCarthy12
  • 3,819
  • 8
  • 28
  • 34
34
votes
2 answers

Launch a totally independent process from Python

I'm trying to launch a completely independent process from python. I can't use something simple like os.startfile since I need to pass arguments. Currently I'm using subprocess.popen which gets me 90% of the way there. args = ["some_exe.exe",…
greenhat
  • 1,061
  • 1
  • 12
  • 19
33
votes
2 answers

subprocess.Popen() error (No such file or directory) when calling command with arguments as a string

I am trying to count the number of lines in a file using Python functions. Within the current directory, while os.system("ls") finds the file, the command subprocess.Popen(["wc -l filename"], stdout=subprocess.PIPE) does not work. Here is my…
user2105632
  • 751
  • 1
  • 7
  • 12
33
votes
3 answers

how to call a program from python without waiting for it to return

is there a way to call a program from python without waiting for it to return? i created a script which copies a program to a directory and runs that program. but when i call the program from python, the python script does not exit until the program…
maranas
  • 1,396
  • 1
  • 12
  • 21
32
votes
5 answers

link several Popen commands with pipes

I know how to run a command using cmd = subprocess.Popen and then subprocess.communicate. Most of the time I use a string tokenized with shlex.split as 'argv' argument for Popen. Example with "ls -l": import subprocess import shlex print…
user940797
31
votes
4 answers

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…
Jdog
  • 10,071
  • 4
  • 25
  • 42
31
votes
3 answers

Creating fstream object from a FILE* pointer

The well known way of creating an fstream object is: ifstream fobj("myfile.txt"); ie. using a filename. But I want to create an ifstream object using a file descriptor. Reason: I want to execute a command using _popen(). _popen() returns the…
Tabrez Ahmed
  • 2,830
  • 6
  • 31
  • 48
29
votes
2 answers

what is the difference between popen() and system() in C

I want to execute a binary within my C code. Which is better to execute with? popen() or system() EDIT: I tried to use system, but the process executing seems to get stuck in the end and does not return to my code. Any suggestions on what to…
Syntax_Error
  • 5,964
  • 15
  • 53
  • 73
29
votes
3 answers

c popen won't catch stderr

I'm trying to use popen() to catch the stderr of a call, but of course it doesn't seem to be doing that. Any ideas? My code looks more or less like this: popen("nedit", "r"); But I'm getting all this garbage about non-utf8 on my screen...
poy
  • 10,063
  • 9
  • 49
  • 74
29
votes
2 answers

What is a practical difference between check_call check_output call, and Popen methods in the subprocess module?

Honestly, I just don't understand the lingo of "non-zero" status to really interpret what's going on or what that means (it wasn't even defined) on help pages. What are some examples of using python to call other scripts in which these processes of…
Tom
  • 919
  • 3
  • 9
  • 22