472,976 Members | 1,427 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,976 software developers and data experts.

How do I use the subprocess module with mswindows?

I'm a developer on the matplotlib project, and I am having trouble with the
subprocess module on windows (Python 2.4.2 on winXP). No trouble to report
with linux. I need to use _subprocess instead of pywin32, but my trouble
exists with either option:

import subprocess
process = subprocess.Popen(['dir'], stderr=subprocess.STDOUT,
stdout=subprocess.PIPE)
stat = process.wait()
print process.stdout.read()
----------------------------------------------------------------------
Traceback (most recent call last):
* File "C:\Documents and Settings\Darren\Desktop\subprocess_test.py",
line 3, in ?
* * process = subprocess.Popen(['dir'], stderr=subprocess.STDOUT,
stdout=subprocess.PIPE)#, stdout=PIPE)
* File "C:\Python24\lib\subprocess.py", line 533, in __init__
* * (p2cread, p2cwrite,
* File "C:\Python24\lib\subprocess.py", line 593, in _get_handles
* * p2cread = self._make_inheritable(p2cread)
* File "C:\Python24\lib\subprocess.py", line 634, in _make_inheritable
* * DUPLICATE_SAME_ACCESS)
TypeError: an integer is required
----------------------------------------------------------------------

If I change my script a bit, I get a different error:

import subprocess
process = subprocess.Popen(['dir'])
stat = process.wait()
print process.stdout.read()
----------------------------------------------------------------------
Traceback (most recent call last):
* File "C:\Documents and Settings\Darren\Desktop\subprocess_test.py",
line 3, in ?
* * process = subprocess.Popen(['dir'])#, stderr=subprocess.STDOUT,
stdout=subprocess.PIPE)#, stdout=PIPE)
* File "C:\Python24\lib\subprocess.py", line 542, in __init__
* * errread, errwrite)
* File "C:\Python24\lib\subprocess.py", line 706, in _execute_child
* * startupinfo)
WindowsError: [Errno 2] The system cannot find the file specified
----------------------------------------------------------------------

Can anyone tell me what I am doing wrong?

Thanks,
Darren
Mar 17 '06 #1
3 5447
Darren Dale wrote:
If I change my script a bit, I get a different error:

import subprocess
process = subprocess.Popen(['dir'])
stat = process.wait()
print process.stdout.read() WindowsError: [Errno 2] The system cannot find the file specified


"dir" is a shell command under Windows, not an executable. to run
commands via the shell, use

process = subprocess.Popen(['dir'], shell=True)

</F>

Mar 17 '06 #2
Fredrik Lundh wrote:
Darren Dale wrote:
If I change my script a bit, I get a different error:

import subprocess
process = subprocess.Popen(['dir'])
stat = process.wait()
print process.stdout.read()

WindowsError: [Errno 2] The system cannot find the file specified


"dir" is a shell command under Windows, not an executable. to run
commands via the shell, use

process = subprocess.Popen(['dir'], shell=True)

</F>


Thank You!
Mar 17 '06 #3
Darren Dale <dd**@cornell.edu> wrote:
import subprocess
process = subprocess.Popen(['dir'], stderr=subprocess.STDOUT,
stdout=subprocess.PIPE)
stat = process.wait()
print process.stdout.read()


You have already gotten the answer to why 'dir' doesn't work for
you, but there is a bug hiding in that code that you might not
notice in simple tests.

You are waiting for your subprocess to complete without reading
away what it prints. That will quickly fill the buffer available
in the pipe between you and the subprocess, and the subprocess
will block. Try calling Popen() with something that prints more
data, like ['find', '/', '-print'] on a Unix box, and you will
notice that problem.

What you should do is:

output = process.stdout.read()
stat = process.wait()
print output

Or, you could use the .communicate() method on the Popen object
instead of .stdout.read().

If you find yourself juggling several subprocesses running in
parallel, producing and/or consuming data "incrementally", or
just trying to handle both sending input to and reading output
from a single subprocess without deadlocking, you may be helped
by using my asyncproc module, which you can download from

http://www.lysator.liu.se/~bellman/d...d/asyncproc.py

I suspect that it only works on Unix, though.
--
Thomas Bellman, Lysator Computer Club, Linköping University, Sweden
"Beware of bugs in the above code; I have ! bellman @ lysator.liu.se
only proved it correct, not tried it." ! Make Love -- Nicht Wahr!
Mar 19 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Michael McGarry | last post by:
Hi, I am just starting to use Python. Does Python have all the regular expression features of Perl? Is Python missing any features available in Perl? Thanks, Michael
4
by: Nicolas Fleury | last post by:
Hi, I want to use the subprocess module (or any standard Python module) to run a process: - which stdout and stderr can each be redirected to any file-like object (no fileno function). - can be...
0
by: Robin Becker | last post by:
After struggling with os.spawnxxx to get a detached process I tried using Pyhton2.4's new subprocess module. I struggled with that as well even when trying to use the creation flags for...
1
by: zloster | last post by:
I'm using Python 2.4.3 for Win32. I was trying to run a few child processes simultaneously in separate threads and get their STDOUT, but the program was leaking memory and I found that it was...
5
by: Grant Edwards | last post by:
I'm trying to use the py-gnuplot module on windows, and have been unable to get it to work reliably under Win2K and WinXP. By default, it uses popen(gnuplotcmd,'w'), but in some situations that...
3
by: Lee | last post by:
Has anyone ran into this problem? I've done extensive googling and research and I cannot seem to find the answer. I downloaded the source for 2.5.1 from python.org compiled and installed it on a...
12
by: bhunter | last post by:
Hi, I've used subprocess with 2.4 several times to execute a process, wait for it to finish, and then look at its output. Now I want to spawn the process separately, later check to see if it's...
23
by: Harishankar | last post by:
Hi, Sorry to start off on a negative note in the list, but I feel that the Python subprocess module is sorely deficient because it lacks a mechanism to: 1. Create non-blocking pipes which can...
25
by: Jeremy Banks | last post by:
Hi. I wondered if anyone knew the rationale behind the naming of the Popen class in the subprocess module. Popen sounds like the a suitable name for a function that created a subprocess, but the...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.