7

I started developing a small pdf to jpg script on a Windows 7 x64 machine with cygwin installed (Python 2.7). The following works perfectly:

import subprocess
filename = "test"
subprocess.check_output('gs -sDEVICE=jpeg -dPDFFitPage -g2800x3620 -o ' + filename + '-2800-%03d.jpg ' + filename + '.pdf', stderr=subprocess.STDOUT)

After pulling this project on my Windows 10 x64 non-Cygwin machine (Python 2.7), this code errors as it no longer recognizes "gs" as a built-in short for ghostscript. So I try the following:

import subprocess
filename = "test"
subprocess.check_output('C:\Program Files\gs\gs9.20\bin\gwin64c.exe -sDEVICE=jpeg -dPDFFitPage -g2800x3620 -o ' + filename + '-2800-%03d.jpg ' + filename + '.pdf', stderr=subprocess.STDOUT)

WindowsError: [Error 2] The system cannot find the file specified

Ok fine, I am a beginner and making some obvious mistake here. Also the "\b" characters in between .20\bin\ are highlighted ... indicating to me I somehow haven't made clear this is a single path.

Things I have also tried (separate runs):

subprocess.check_output(r'C:\Program Files\gs\gs9.20\bin\gwin64c.exe -sDEVICE=jpeg -dPDFFitPage -g2800x3620 -o ' + filename + '-2800-%03d.jpg ' + filename + '.pdf', stderr=subprocess.STDOUT')

subprocess.check_output("C:\Program Files\gs\gs9.20\bin\gwin64c.exe -sDEVICE=jpeg -dPDFFitPage -g2800x3620 -o ' + filename + '-2800-%03d.jpg ' + filename + '.pdf', stderr=subprocess.STDOUT/s", shell=True)

I have read:

to no avail

The error given always is:

Traceback (most recent call last):
File "C:/Python/Project/projectx/manual_conversion/manual_conversion.py", line 16, in <module>
subprocess.check_output(r'C:\Program Files\gs\gs9.20\bin\gwin64c.exe -sDEVICE=jpeg -dPDFFitPage -g2800x3620 -o ' + filename + '-2800-%03d.jpg ' + filename + '.pdf', stderr=subprocess.STDOUT)
File "C:\Python\Python 2.7.12\lib\subprocess.py", line 567, in check_output
process = Popen(stdout=PIPE, *popenargs, **kwargs)
File "C:\Python\Python 2.7.12\lib\subprocess.py", line 711, in __init__
errread, errwrite)
File "C:\Python\Python 2.7.12\lib\subprocess.py", line 959, in _execute_child
startupinfo)
WindowsError: [Error 2] The system cannot find the file specified

I fully understand this is rookie stuff + switching to unix would make my life easier - but in my corporate environment I don't fully have that option.

I have also tried:

subprocess.check_output(r'C:\Program Files\gs\gs9.20\bin\gwin64c.exe -sDEVICE=jpeg -dPDFFitPage -g2800x3620 -o 2800-%03d.jpg test.pdf')

Since I thought maybe my variable concatenation was breaking the command ... but this again yields the same error.

EDIT 2: Passing the full argument as a list

subprocess.check_output([r'C:\Program Files\gs\gs9.20\bin\gswin64c.exe -sDEVICE=jpeg -dPDFFitPage -g2800x3620 -o 2800-%03d.jpg test.pdf'])

Yields the same error. But this:

subprocess.check_output([r'C:\Program Files\gs\gs9.20\bin\gswin64c.exe'])

Does start up ghostscript in Windows. Thank you @lit for making me try things, now the new problem is how to pass options to subprocess' check_output executable.

Still bothers me how easy it worked on a Windows system simply because it had cygwin installed that somehow modified cmd namespace to recognize "gs"...

3
  • 3
    Please read the documentation on subprocess.check_output(). The first parameter should be a list. What happens if you try subprocess.check_output([r"C:\Program Files\gs\gs9.20\bin\gwin64c.exe", "-sDEVICE=jpeg", "-dPDFFitPage", "-g2800x3620", "-o", "2800-%03d.jpg test.pdf"]) Commented Dec 17, 2016 at 14:46
  • Also, did you intend to pass %03d in what appears to be an output filename? Commented Dec 17, 2016 at 14:53
  • @lit thank you, even though it did not provide assistance in the full command workings - I skipped a step in trying to solve the problem I now see! Now to pass options... The %03d is a decimal postfix, ghostscript syntax, the first image in your comment would be called 2800-001, where the second would be 2800-002 and so forth. Commented Dec 17, 2016 at 18:37

2 Answers 2

6

The path contains a space here Program Files. Typically, a space means end of the path. Using quotes " around the path tells the operating system not to consider the space as the end of the path but to take all the text in between the quotes as the path. So just add quotes:

subprocess.check_output([r'"C:\Program Files\gs\gs9.20\bin\gswin64c.exe" -sDEVICE=jpeg -dPDFFitPage -g2800x3620 -o 2800-%03d.jpg test.pdf']) 
Sign up to request clarification or add additional context in comments.

Comments

1

I was having a similar issue with subprocess.call() & the only way to get it working was to split the whole command into a proper list of executable & args, e.g. subprocess.call(['mvn', '-f', f'{path_to}/pom.xml', 'clean', 'compile']).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.