Running lpr on windows from python

Chris Angelico rosuav at gmail.com
Wed Apr 20 10:07:26 EDT 2016


On Wed, Apr 20, 2016 at 11:57 PM, loial <jldunn2000 at gmail.com> wrote:
> I am trying to run lpr from python 2.7.10 on windows
>
> However I always get the error
> 'C:/windows/system32/lpr.exe ' is not recognized as an internal or external command,
> operable program or batch file.
>
> Even though typing the same at the  command prompt works OK
>
>
> Any ideas?
>
> I am using subprocess as follows
>
> process = subprocess.Popen(commandline, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
>
> where command line is
> C:/windows/system32/lpr.exe -S 172.28.84.38 -P RAW C:/john/myfile

You''re running that through the shell, which means you have to abide
by shell rules. I don't have a Windows handy, but I'm pretty sure its
shell isn't happy with forward slashes in the command line; I might be
wrong there.

My recommendation: Split that into separate arguments, pass them as a
list, and remove shell=True. And unless you need to be completely
explicit for some reason (eg to protect against path-based exploits),
cut the first argument to just "lpr" and let the binary be found
anywhere.

ChrisA



More information about the Python-list mailing list