Fix Python – 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?
….

Fix Python – live output from subprocess command

I’m using a python script as a driver for a hydrodynamics code. When it comes time to run the simulation, I use subprocess.Popen to run the code, collect the output from stdout and stderr into a subprocess.PIPE — then I can print (and save to a log-file) the output information, and check for any errors. The problem is, I have no idea how the c….

Fix Python – read subprocess stdout line by line

My python script uses subprocess to call a linux utility that is very noisy. I want to store all of the output to a log file and show some of it to the user. I thought the following would work, but the output doesn’t show up in my application until the utility has produced a significant amount of output.
#fake_utility.py, just generates lots of ….

Fix Python – 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 ““, line 1, in ?
File “/build/toolchain/mac32/python-2.4.3/lib/python2.4/subprocess.py”, line ….

Fix Python – 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….

Fix Python – Actual meaning of ‘shell=True’ in subprocess

I am calling different processes with the subprocess module. However, I have a question.
In the following codes:
callProcess = subprocess.Popen([‘ls’, ‘-l’], shell=True)

and
callProcess = subprocess.Popen([‘ls’, ‘-l’]) # without shell

Both work. After reading the docs, I came to know that shell=True means executing the code through the shell. S….

Fix Python – 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:” + my_env[“PATH”]
subprocess.Popen(my_command, env=my_env)

I’ve got a gut feeling that there’s a better way; does it look alright?
….