Issue with subprocess Module

Dave Angel davea at ieee.org
Tue Apr 7 10:18:11 EDT 2009


Tim Golden wrote:
> <div class="moz-text-flowed" style="font-family: -moz-fixed">Dave 
> Angel wrote:
>> tarun wrote:
>>> Hello All,
>>>
>>> I've a batch file to be  invoke using a python script. The batch 
>>> file has
>>> pause, and the time, I need to send some command to the batch file 
>>> from my
>>> scripts. I placed both, the batch file (test.bat) and the python script
>>> (test.py) in the same folder. And executed 'test.py'
>>>
>>> (Please find the source files and error below).
>>>
>>> *I get the following error:*
>>> Traceback (most recent call last):
>>>   File "<string>", line 74, in run_nodebug
>>>   File "D:\test.py", line 4, in <module>
>>>     proc = subprocess.Popen(my_bat,stdin=subprocess.PIPE)
>>>   File "C:\Python25\lib\subprocess.py", line 588, in __init__
>>>     errread, errwrite) = self._get_handles(stdin, stdout, stderr)
>>>   File "C:\Python25\lib\subprocess.py", line 717, in _get_handles
>>>     c2pwrite = self._make_inheritable(c2pwrite)
>>>   File "C:\Python25\lib\subprocess.py", line 746, in _make_inheritable
>>>     DUPLICATE_SAME_ACCESS)
>>> WindowsError: [Error 6] The handle is invalid
>>>
>>> *Python Script:*
>>> *test.py*
>>> import subprocess,os
>>> my_bat = os.getcwd()+'\\test.bat'
>>> proc = subprocess.Popen(my_bat,stdin=subprocess.PIPE)
>>> input = '\n'
>>> proc.communicate(input)
>>>
>>> *Batch File*
>>> *test.bat*
>>> echo "START'
>>> pause
>>> echo 'END'
>>> Please help me with this issue.
>>>
>>> Thanks In Advance,
>>> Tarun
>>>
>>>   
>> subprocess.Popen() is expecting the name of a program, which should 
>> normally have an extension of .exe   You're handing it a .bat file, 
>> which is not executable.  It only executes in the context of a 
>> command interpreter (shell), such as cmd.exe
>>
>> You can probably do what you want by running "cmd.exe" and passing it 
>> "test.bat" as a parameter
>
> Sounds reasonable, but isn't actually true. This works fine:
>
> <code>
> import subprocess
>
> open ("t.bat", "w").write ("echo hello")
> subprocess.Popen ("t.bat")
>
> </code>
>
> TJG
>
> </div>
>

The docs of Popen() state that it uses CreateProcess() on Windows, so I 
didn't even try it.  Thanks for informing me it works.  I see now the 
COMSPEC manipulation in _execute_child(), but I'm still puzzled.  When I 
step through, it skips that part because we didn't specify shell= as an 
argument.   It still had not put the cmd.exe /c into the args variable 
when it called CreateProcess().  So is the Python CreateProcess more 
than a thin wrapper around Windows version?

Another bug in this program is that it does not include quotes around 
the program name.  If the current directory has a space in it, and an 
appropriately named executable happens to be in the parent directory of 
the one with the space, it'll get run instead of the batch.  For 
example, if the current directory  is   c:\source code\test    and 
there's a file in the route called    c:\source.exe     then it'll get run.

But what about the OP problem?  I now see it runs okay for me, both 
stand-alone and inside Komodo.  But he's getting an exception inside 
make_inheritable(), which is apparently being passed an invalid handle.

Runs OK for me, in Python 2.6 running under XP, SP3.      2.6.1 
(r261:67517, Dec  4 2008, 16:51:00) [MSC v.1500 32 bit (Intel)]





More information about the Python-list mailing list