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
418
votes
9 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
  • 4,481
  • 2
  • 17
  • 8
243
votes
2 answers

How can I specify working directory for a subprocess

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
  • 17,126
  • 39
  • 105
  • 141
225
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
  • 19,499
  • 5
  • 29
  • 47
152
votes
4 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
  • 10,755
  • 23
  • 83
  • 144
129
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
  • 5,293
  • 7
  • 42
  • 78
108
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
  • 26,348
  • 30
  • 111
  • 168
102
votes
4 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,914
  • 3
  • 19
  • 18
83
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
  • 831
  • 1
  • 6
  • 5
81
votes
4 answers

python subprocess Popen environment PATH?

Suppose there's an executable and a Python script to launch it, and they're located in 'sibling' subdirectories, e.g. /tmp/subdir1/myexecutable /tmp/subdir2/myscript.py If in /tmp and running python subdir2/myscript.py with a relative path to…
wim
  • 338,267
  • 99
  • 616
  • 750
81
votes
7 answers

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

I have 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,813
  • 9
  • 44
  • 65
70
votes
5 answers

Popen error: "[Errno 2] No such file or directory" when calling shell function

I have some custom commands. This 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
  • 3,343
  • 7
  • 29
  • 33
63
votes
5 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
  • 11,709
  • 17
  • 81
  • 125
59
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
  • 5,293
  • 7
  • 42
  • 78
57
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
  • 56,214
  • 69
  • 228
  • 381
55
votes
9 answers

Calling the "source" command from subprocess.Popen

I have a .sh script that I call with source the_script.sh. Calling this regularly is fine. However, I am trying to call it from my python script, through subprocess.Popen. Calling it from Popen, I am getting the following errors in the following…
coffee
  • 595
  • 1
  • 5
  • 5
1
2 3
99 100