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
13
votes
1 answer

How do I launch a file in its default program, and then close it when the script finishes?

Summary I have wxPython GUI which allows the user to open files to view. Currently I do this with os.startfile(). However, I've come to learn that this is not the best method, so I'm looking to improve. The main drawback of startfile() is that I…
wnnmaw
  • 5,444
  • 3
  • 38
  • 63
13
votes
4 answers

What permissions are required for subprocess.Popen?

The following code: gb = self.request.form['groupby'] typ = self.request.form['type'] tbl = self.request.form['table'] primary = self.request.form.get('primary', None) if primary is not None: create = False else: create = True mdb =…
Jon Hadley
  • 5,196
  • 8
  • 41
  • 65
12
votes
2 answers

Reading popen results in C++

I am writing a C++ application and I need to read the result of a system command. I am using popen() more or less as shown here: const int MAX_BUFFER = 2048; string cmd="ls -l"; char buffer[MAX_BUFFER]; FILE *stream =…
Stefano
  • 3,981
  • 8
  • 36
  • 66
12
votes
3 answers

Python: How to read stdout non blocking from another process?

During the runtime of a process I would like to read its stdout and write it to a file. Any attempt of mine however failed because no matter what I tried as soon as I tried reading from the stdout it blocked until the process finished. Here is a…
Woltan
  • 13,723
  • 15
  • 78
  • 104
12
votes
3 answers

Python popen() - communicate( str.encode(encoding="utf-8", errors="ignore") ) crashes

Using Python 3.4.3 on Windows. My script runs a little java program in console, and should get the ouput: import subprocess p1 = subprocess.Popen([ ... ], stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True) out, err =…
user136036
  • 11,228
  • 6
  • 46
  • 46
12
votes
5 answers

subprocess.Popen.stdout - reading stdout in real-time (again)

Again, the same question. The reason is - I still can't make it work after reading the following: Real-time intercepting of stdout from another process in Python Intercepting stdout of a subprocess while it is running How do I get 'real-time'…
Vladimir Keleshev
  • 13,753
  • 17
  • 64
  • 93
12
votes
5 answers

Linux time sample based profiler

short version: Is there a good time based sampling profiler for Linux? long version: I generally use OProfile to optimize my applications. I recently found a shortcoming that has me wondering. The problem was a tight loop, spawning c++filt to…
deft_code
  • 57,255
  • 29
  • 141
  • 224
12
votes
3 answers

subprocess.Popen simple code does not allow me to perform a cd (change directory)

I'm trying to use a Python script to change directory, but I am getting an error. The python code: import subprocess p = subprocess.Popen(['cd', '~'], stdout=subprocess.PIPE) output = p.communicate() print output I get this error: File…
user3512904
12
votes
1 answer

Is subprocess.Popen not thread safe?

The following simple script hangs on the subprocess.Popen call intermittently (roughly 30% of the time). Unless use_lock = True, and then it never hangs, leading me to believe subprocess is not thread safe! The expected behavior is script finishes…
Roman
  • 503
  • 4
  • 15
12
votes
3 answers

Is there any way of stopping _popen opening a dos window?

I am using _popen to start a process to run a command and gather the output This is my c++ code: bool exec(string &cmd, string &result) { result = ""; FILE* pipe = _popen(cmd.c_str(), "rt"); if (!pipe) return(false); char…
user236520
11
votes
1 answer

Deadlock in Python's subprocess popen

I'm having a problem where popen is deadlocking. Specifically, the thread (not the main thread) that runs the popen is stuck at: File: "/usr/lib/python2.7/subprocess.py", line 679, in __init__ errread, errwrite) File:…
UsAaR33
  • 3,536
  • 2
  • 34
  • 55
11
votes
2 answers

Why is standard output from subprocess (redirected to unbuffered file) being buffered?

From http://docs.python.org/library/functions.html#open The optional bufsize argument specifies the file’s desired buffer size: 0 means unbuffered, 1 means line buffered, any other positive value means use a buffer of (approximately) that…
Piotr Dobrogost
  • 41,292
  • 40
  • 236
  • 366
11
votes
1 answer

How to push local files to github using python? (or post a commit via Python)

What options are there for commiting and pushing files to github from python? Here are three methods I thought should be feasible so attempted in order: Use pygithub: (Github's python API) to send push requests to my repository. Failed because I…
Jesse Reza Khorasanee
  • 3,140
  • 4
  • 36
  • 53
11
votes
3 answers

How do I get back the option string using argparse?

parser = argparse.ArgumentParser() parser.add_argument("-p", "--pattern", help="Pattern file") args = parser.parse_args() Now is it possible to get back the string "--pattern" from args? I need the string so that I can construct a cmd list to pass…
user80551
  • 1,134
  • 2
  • 15
  • 26
11
votes
1 answer

Python encoding for pipe.communicate

I'm calling pipe.communicate from Python's subprocess module from Python 2.6. I get the following error from this code: from subprocess import Popen pipe = Popen(cwd) pipe.communicate( data ) For an arbitrary cwd, and where data that contains…
Brian M. Hunt
  • 81,008
  • 74
  • 230
  • 343