Success with subprocess communicate on Windows?

Wolfgang Maier wolfgang.maier at biologie.uni-freiburg.de
Wed Jul 2 19:22:50 EDT 2014


Terry Reedy <tjreedy <at> udel.edu> writes:

> 
> On 7/2/2014 12:33 AM, Tim Roberts wrote:
> > Terry Reedy <tjreedy <at> udel.edu> wrote:
> >>
> > You need to use
> >      s.check_output("pyflakes c:\\programs\\python34\\lib\\turtle.py")
> > or
> >      s.check_output(r"pyflakes c:\programs\python34\lib\turtle.py")
> 
> Now I get "Command 'pyflakes c:\programs\python34\lib\turtle.py' returns 
> non-zero exit status 1" on both. On Idle, as least, a command-prompt 
> window is flashed/displayed. It makes no sense to me that in the command 
> interpreter,
> 'pyflakes c:\\programs\\python34\\lib' works and
> 'pyflakes c:\\programs\\python34\\lib\\turtle.py' returns status 1.
> whereas both (with quotes elided and undoubled \) work at the command 
> prompt.
> 

Finally found out what the problem is:
When I'm running your command using the cmd console, I get this output:

c:\python34\lib\turtle.py:571: local variable 'rgb' is assigned to but never
used
c:\python34\lib\turtle.py:2936: local variable 'a21' is assigned to but
never used
c:\python34\lib\turtle.py:3590: local variable 'dummy' is assigned to but
never used
c:\python34\lib\turtle.py:3786: undefined name 'mainloop'
c:\python34\lib\turtle.py:3969: undefined name 'mainloop'
c:\python34\lib\turtle.py:3973: undefined name 'isdown'
c:\python34\lib\turtle.py:3974: undefined name 'pu'
c:\python34\lib\turtle.py:3976: undefined name 'pd'
..

now look at the exit code:
echo %errorlevel%
1

ah, pyflakes does exit with a non-zero exit code when it finds errors in
your file!!

Now, using subprocess.check_output in IDLE:

>>> msg=subprocess.check_output(r'pyflakes c:\python34\lib\turtle.py')
Traceback (most recent call last):
  File "<pyshell#45>", line 1, in <module>
    msg=subprocess.check_output(r'pyflakes c:\python34\lib\turtle.py')
  File "C:\Python34\lib\subprocess.py", line 618, in check_output
    raise CalledProcessError(retcode, process.args, output=output)
subprocess.CalledProcessError: Command 'pyflakes c:\python34\lib\turtle.py'
returned non-zero exit status 1

as you said, but logical since (taken from the docs):

"subprocess.check_output(args, *, input=None, stdin=None, stderr=None,
shell=False, universal_newlines=False, timeout=None) 
Run command with arguments and return its output.

If the return code was non-zero it raises a CalledProcessError. The
CalledProcessError object will have the return code in the returncode
attribute and any output in the output attribute."

and in fact:
>>> try:
	msg=subprocess.check_output(r'pyflakes c:\python34\lib\turtle.py')
except subprocess.CalledProcessError as e:
	print (e.output[:81])

	
b"c:\\python34\\lib\\turtle.py:571: local variable 'rgb' is assigned to but
never used"

So, everything's just fine except that it may be more convenient to use
Popen().communicate() to avoid raising the error in the first place :)

Hope it helps this time,
Wolfgang




More information about the Python-list mailing list