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 > output I read the documentation and it says that call() … Read more

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 C:\mytool\tool.exe in the directory D:\test\local? How do I set the working directory for a sub-process? 2 Answers 2

Constantly print Subprocess output while process is running

To launch programs from my Python-scripts, I’m using the following method: def execute(command): process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) output = process.communicate()[0] exitCode = process.returncode if (exitCode == 0): return output else: raise ProcessException(command, exitCode, output) So when i launch a process like Process.execute(“mvn clean install”), my program waits until the process is finished, and … Read more

Retrieving the output of subprocess.call() [duplicate]

This question already has answers here: Store output of subprocess.Popen call in a string [duplicate] (15 answers) Closed 3 years ago. How can I get the output of a process run using subprocess.call()? Passing a StringIO.StringIO object to stdout gives this error: Traceback (most recent call last): File “<stdin>”, line 1, in <module> File “/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/subprocess.py”, … Read more

How do I pass a string into subprocess.Popen (using the stdin argument)?

If I do the following: import subprocess from cStringIO import StringIO subprocess.Popen([‘grep’,’f’],stdout=subprocess.PIPE,stdin=StringIO(‘one\ntwo\nthree\nfour\nfive\nsix\n’)).communicate()[0] I get: Traceback (most recent call last): File “<stdin>”, line 1, in ? File “/build/toolchain/mac32/python-2.4.3/lib/python2.4/subprocess.py”, line 533, in __init__ (p2cread, p2cwrite, File “/build/toolchain/mac32/python-2.4.3/lib/python2.4/subprocess.py”, line 830, in _get_handles p2cread = stdin.fileno() AttributeError: ‘cStringIO.StringI’ object has no attribute ‘fileno’ Apparently a cStringIO.StringIO object doesn’t quack … Read more

How to hide output of subprocess

I’m using eSpeak on Ubuntu and have a Python 2.7 script that prints and speaks a message: import subprocess text=”Hello World.” print text subprocess.call([‘espeak’, text]) eSpeak produces the desired sounds, but clutters the shell with some errors (ALSA lib…, no socket connect) so i cannot easily read what was printed earlier. Exit code is 0. … Read more