FAQ
Windows version of Python 2.6.4: Is there any way to determine if
subprocess.Popen() fails when using shell=True?

Popen() successfully fails when shell=False
import subprocess
p = subprocess.Popen( 'Nonsense.application', shell=False )
Traceback (most recent call last):
File "<pyshell#258>", line 1, in <module>
p = subprocess.Popen( 'Nonsense.application' )
File "C:\Python26\lib\subprocess.py", line 621, in __init__
errread, errwrite)
File "C:\Python26\lib\subprocess.py", line 830, in
_execute_child
startupinfo)
WindowsError: [Error 2] The system cannot find the file specified

But when shell=True, there appears to be no way to determine if a
Popen() call was successful or not.
p = subprocess.Popen( 'Nonsense.application', shell=True )
p
<subprocess.Popen object at 0x0275FF90>
p.pid
6620
p.returncode
Regards,
Malcolm

Search Discussions

  • Chris Rebert at May 17, 2010 at 8:59 pm

    On Mon, May 17, 2010 at 1:45 PM, wrote:
    Windows version of Python 2.6.4: Is there any way to determine if
    subprocess.Popen() fails when using shell=True?

    Popen() successfully fails when shell=False
    import subprocess
    p = subprocess.Popen( 'Nonsense.application', shell=False )
    Traceback (most recent call last):
    ?File "<pyshell#258>", line 1, in <module>
    ? ?p = subprocess.Popen( 'Nonsense.application' )
    ?File "C:\Python26\lib\subprocess.py", line 621, in __init__
    ? ?errread, errwrite)
    ?File "C:\Python26\lib\subprocess.py", line 830, in
    _execute_child
    ? ?startupinfo)
    WindowsError: [Error 2] The system cannot find the file specified

    But when shell=True, there appears to be no way to determine if a
    Popen() call was successful or not.
    p = subprocess.Popen( 'Nonsense.application', shell=True )
    p
    <subprocess.Popen object at 0x0275FF90>
    p.pid
    6620
    p.returncode
    I don't have a Windows box to test on, but have you tried p.wait()?
    Worked for me on *nix:
    $ python
    Python 2.6.5 (r265:79063, May 11 2010, 13:15:13)
    [GCC 4.2.1 (Apple Inc. build 5659)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    from subprocess import Popen
    a = Popen("nonsense", shell=True)
    /bin/sh: nonsense: command not found
    a.returncode
    a.wait()
    127
    a.returncode
    127

    Cheers,
    Chris
  • Python at May 19, 2010 at 5:00 pm

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedMay 17, '10 at 8:45p
activeMay 19, '10 at 5:00p
posts3
users2
websitepython.org

2 users in discussion

Python: 2 posts Chris Rebert: 1 post

People

Translate

site design / logo © 2022 Grokbase