This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Subprocess.Popen output fails on Windows
Type: behavior Stage:
Components: IO, Windows Versions: Python 2.6
process
Status: closed Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: ac.james, amaury.forgeotdarc, pjenvey
Priority: normal Keywords:

Created on 2009-05-26 00:26 by ac.james, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (5)
msg88327 - (view) Author: Alex James (ac.james) Date: 2009-05-26 00:45
When calling p=subprocess.Popen(findstr "string" filename, stdout=PIPE)
both p.stdout.read() and p.communicate()[0] are returning None even when
the shell process has output (ie string was found in filename).  
Further, redirecting stdout to a file will write an empty file.  

I've got this result from running in script and on IDLE command line
with Python 2.6.2 on windows Vista home premuim and windows XP SP2
systems.  

Thinking this may be related to issue 1707753 or 1124861 I tried the
putting all streams to pipes workaround, but that also failed here.  

Using subprocess.call with stdout to file did work.  

This issue is primarily about the lack of cross-platform compatability
that the subprocess module was supposed to have, on unix systems the
base program I'm working with can call grep from any of os.popen,
popen2, or subprocess.Popen and work just fine.
msg88329 - (view) Author: Philip Jenvey (pjenvey) * (Python committer) Date: 2009-05-26 02:31
Exactly what command line are you passing to subprocess? Does stderr 
contain anything?
msg88336 - (view) Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * (Python committer) Date: 2009-05-26 07:22
The following works for me:

>>> import subprocess
>>> p=subprocess.Popen('findstr "disk" c:\\boot.ini',
stdout=subprocess.PIPE)
>>> print p.communicate()[0]
default=multi(0)disk(0)rdisk(0)partition(1)\WINDOWS
multi(0)disk(0)rdisk(0)partition(1)\WINDOWS="Microsoft Windows XP
Professional" /fastdetect /NoExecute=OptOut

Tested with python2.5 on Windows XP, and python2.6 on Windows 2000
msg88377 - (view) Author: Alex James (ac.james) Date: 2009-05-26 17:58
>>> import subprocess
>>> fileName = 'test_a5.py.out'
>>> locator = 'step 5200 '
>>> p = subprocess.Popen('findstr /O /B /C:"' + locator + '" '+
fileName, stdout=subprocess.PIPE, shell=True)
>>> print p.stdout.read()

>>> print p.communicate()[0]

And stderr is also returning None when similarly referenced.  I'm using
relative file paths since the script and data files are all kept in the
same directory, and to avoid escaping backslashes.  
When the same (composited) line is typed into cmd.exe it returns properly
223473:step 5200 potentialEnergySum -2643.62601773
msg88379 - (view) Author: Alex James (ac.james) Date: 2009-05-26 18:36
Um, nevermind.  
I completely missed cwd=os.path.split(sys.argv[0])[0] so the shell
command wasn't operating in the same relative path as the script.  

And that never mattered on Unix because we always ran from commandline,
not IDLE, so the shell inherited the commandline directory where we ran
the script.  

Now don't I feel silly for spending three days on this.
History
Date User Action Args
2022-04-11 14:56:49adminsetgithub: 50357
2009-05-26 18:36:58ac.jamessetstatus: open -> closed

messages: + msg88379
2009-05-26 17:58:31ac.jamessetmessages: + msg88377
2009-05-26 07:22:26amaury.forgeotdarcsetnosy: + amaury.forgeotdarc
messages: + msg88336
2009-05-26 02:31:32pjenveysetnosy: + pjenvey
messages: + msg88329
2009-05-26 00:45:53ac.jamessetmessages: + msg88327
title: Pipes fail to return subprocess output on Windows -> Subprocess.Popen output fails on Windows
2009-05-26 00:26:47ac.jamescreate