Subprocess Popen and PIPE in Python

2019-06-27 09:59发布

问题:

The following code prints an empty line as an output which is false. The problem is not in the permissions, since I tested the command with 777 permissions for the pdf -file. How can you fix the command to give a right output?

import subprocess
from subprocess import PIPE, Popen
output = Popen(['pdftotext', '/home/aal/Desktop/lkn_pdf/appa.pdf'], stdout=PIPE).communicate()[0]

回答1:

pdftotext creates a file by default. To send the result to standard output, use:

pdftotext file.pdf -

or in Python:

output = Popen(['pdftotext', '/home/aal/Desktop/lkn_pdf/appa.pdf', '-'], stdout=PIPE).communicate()[0]