4

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]
Léo Léopold Hertz 준영
  • 134,464
  • 179
  • 445
  • 697
  • 1
    unrelated: In Python 2.7+, you could write it as `output = check_output(['pdftotext', '/home/aal/Desktop/lkn_pdf/appa.pdf', '-'])` if you want to raise an exception for non-zero exit status. – jfs Apr 25 '15 at 23:02
  • @J.F.Sebastian I think your comment should be an answer. – Léo Léopold Hertz 준영 Apr 26 '15 at 09:11
  • the comment is for visitors from google due to the question title. But `check_output` vs. `.communicate` has nothing to do with your real issue: providing output filename as `-` so that `pdftotext` writes to the pipe (its stdout) and that part is already answered. – jfs Apr 26 '15 at 15:18

1 Answers1

6

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]
interjay
  • 107,303
  • 21
  • 270
  • 254