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.

2219 questions
326
votes
8 answers

Python subprocess/Popen with a modified environment

I believe that running an external command with a slightly modified environment is a very common case. That's how I tend to do it: import subprocess, os my_env = os.environ my_env["PATH"] = "/usr/sbin:/sbin:" +…
Oren_H
  • 3,571
  • 2
  • 15
  • 8
202
votes
1 answer

How can I specify working directory for popen

Is there a way to specify the running directory of command in Python's subprocess.Popen()? For example: Popen('c:\mytool\tool.exe', workingdir='d:\test\local') My Python script is located in C:\programs\python Is is possible to run…
icn
  • 14,804
  • 36
  • 98
  • 131
194
votes
2 answers

What's the difference between subprocess Popen and call (how can I use them)?

I want to call an external program from Python. I have used both Popen() and call() to do that. What's the difference between the two? My specific goal is to run the following command from Python. I am not sure how redirects work. ./my_script.sh >…
varunl
  • 16,085
  • 5
  • 27
  • 46
118
votes
5 answers

How to use subprocess popen Python

Since os.popen is being replaced by subprocess.popen, I was wondering how would I convert os.popen('swfdump /tmp/filename.swf/ -d') to subprocess.popen() I tried: subprocess.Popen("swfdump /tmp/filename.swf -d") subprocess.Popen("swfdump %s -d" %…
Stupid.Fat.Cat
  • 8,623
  • 15
  • 62
  • 118
100
votes
4 answers

Why does Popen.communicate() return b'hi\n' instead of 'hi'?

Can someone explain why the result I want, "hi", is preceded with a letter 'b' and followed with a newline? I am using Python 3.3 >>> import subprocess >>> print(subprocess.Popen("echo hi", shell=True, …
imagineerThat
  • 4,293
  • 7
  • 30
  • 61
86
votes
3 answers

Is it possible to run function in a subprocess without threading or writing a separate file/script.

import subprocess def my_function(x): return x + 100 output = subprocess.Popen(my_function, 1) #I would like to pass the function object and its arguments print output #desired output: 101 I have only found documentation on opening…
wroscoe
  • 1,547
  • 2
  • 16
  • 17
84
votes
7 answers

Python popen command. Wait until the command is finished

I have a script where I launch with popen a shell command. The problem is that the script doesn't wait until that popen command is finished and go continues right away. om_points = os.popen(command, "w") ..... How can I tell to my Python script to…
michele
  • 24,748
  • 27
  • 92
  • 146
73
votes
7 answers

Launch a shell command with in a python script, wait for the termination and return to the script

I've a python script that has to launch a shell command for every file in a dir: import os files = os.listdir(".") for f in files: os.execlp("myscript", "myscript", f) This works fine for the first file, but after the "myscript" command has…
Davide Gualano
  • 12,155
  • 9
  • 43
  • 63
64
votes
4 answers

Killing a process created with Python's subprocess.Popen()

Here is my thought: First of all, I created a process by using subprocess.Popen Second, after certain amount of time, I tried to kill it by Popen.kill() import subprocess import os, signal import time proc1 = subprocess.Popen("kvm -hda…
user495511
  • 641
  • 1
  • 5
  • 5
63
votes
4 answers

python subprocess Popen environment PATH?

I'm confused about how subprocess searches for the executable when using Popen(). It works if given absolute paths to the child process, but I'm trying to use relative paths. I've found that if I set the environment variable PYTHONPATH then I can…
wim
  • 266,989
  • 79
  • 484
  • 630
56
votes
3 answers

output the command line called by subprocess?

I'm using the subprocess.Popen call, and in another question I found out that I had been misunderstanding how Python was generating arguments for the command line. My Question Is there a way to find out what the actual command line was? Example…
Brian Postow
  • 10,227
  • 14
  • 69
  • 113
53
votes
4 answers

Popen error: [Errno 2] No such file or directory

I have some custom commands. # works subprocess.Popen(['python'], stdout=subprocess.PIPE) But if I have my own system commands like deactivate, I get that error Traceback (most recent call last): File "runner2.py", line 21, in
user1012451
  • 2,773
  • 7
  • 27
  • 30
48
votes
3 answers

How to write to stdout AND to log file simultaneously with Popen?

I am using Popen to call a shell script that is continuously writing its stdout and stderr to a log file. Is there any way to simultaneously output the log file continuously (to the screen), or alternatively, make the shell script write to both the…
imagineerThat
  • 4,293
  • 7
  • 30
  • 61
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,009
  • 6
  • 28
  • 31
45
votes
1 answer

Opening a process with Popen and getting the PID

I'm working on a nifty little function: def startProcess(name, path): """ Starts a process in the background and writes a PID file returns integer: pid """ # Check if the process is already running status, pid =…
Hubro
  • 48,322
  • 60
  • 196
  • 344
1
2 3
99 100