2

I set up a subprocess.Popen to generate a pdf through pdflatex. Code snippet:

process = subprocess.Popen(command, stdout=subprocess.PIPE, shell=True)
output, err = process.communicate()

print output
print err

It works just fine, but the problem is the error message. If pdflatex doesn't manage to generate a file, e.g. I get the message "Fatal error occured, no output PDF file produced!" at the end of the printed output, I still get "None" printed out as the err.

Any insight would be appreciated

Edit: Adding stderr=subprocess.PIPE helps. I don't get "None" anymore, but I do get a blank error message regardless of whether or not the generation of the pdf is successful. It now looks like this:

process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
same as above
Bence Kaulics
  • 7,066
  • 7
  • 33
  • 63
user2875994
  • 195
  • 4
  • 13
  • @padraic cunningham Removing that just throws me errors like "No such file or directory" (which is nonsense since it finds it with shell=True). – user2875994 Oct 23 '14 at 00:16
  • @padraic cunningham shell=True isn't part of the command. I'm not sure how what you're saying is addressing the problem :/ command is just a string I've put together that runs pdflatex on the file. It works perfectly fine. – user2875994 Oct 23 '14 at 00:21
  • @padraic cunningham Well I did that: [link](http://pastebin.com/231BGssL), same result. – user2875994 Oct 23 '14 at 00:26
  • As mentioned in the edit above, a blank error message. It has no content. I don't get "None", it's just blank. And it's blank regardless of whether or not the pdf is successfully generated. – user2875994 Oct 23 '14 at 00:31
  • @padraic cunningham I'm a student doing an assignment. We're given the line `out, err = proc.communicate()` and then told that it gives us the output in out and errors in err. We're then told to print the output and errors out in the terminal. So it's very specific. – user2875994 Oct 23 '14 at 00:40
  • @padraic cunningham ...you get actual error messages displayed? Because if so what's wrong in this code: [link](http://pastebin.com/rM0H2i8P). The relevant bit is at the end (commented out the bit I tried without shell). – user2875994 Oct 23 '14 at 00:47
  • where error messages are printed (stdout/stderr/directly to console or not at all), and what generates them (shell, the command itself) may depend on many things e.g., [Capture “Segmentation fault” message for a crashed subprocess: no out and err after a call to communicate()](http://stackoverflow.com/q/22250893/4279). You shouldn't dismiss offhand whether `shell=True` helps or not in your particular case. In general, use `shell=False` unless you know that you need `shell=True` (less moving parts, might be a slightly faster, and more secure if any part of the command is user generated). – jfs Oct 24 '14 at 14:47
  • how do you know the process has failed (`process.returncode`?)? Why do you think that you should get error message? Do you get the error message if you pipe `pdflatex` output via `cat`: `pdflatex ... |& cat`? Or if the output is redirected to a file (in the file itself) `pdflatex ... >file 2>stderr_file`? – jfs Oct 24 '14 at 14:51
  • @J.F.Sebastian It's actually just that pdflatex doesn't write to stderr. The assignment text I was following was just.. well, wrong. You have to search stdout for the error and grab it. – user2875994 Oct 29 '14 at 22:49

1 Answers1

1

Try this:

fout = open("temp.txt", "w")
process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=fout, shell=True)

I prefer subprocess.check_output(), more convenient.

Swing
  • 858
  • 1
  • 8
  • 21
  • That gives me a blank "temp.txt" file (I assume stderr=fout is supposed to write any errors to fout). – user2875994 Oct 23 '14 at 09:11
  • Do you have to use subprocess.PIPE? try STDOUT. may be it's not a error message. – Swing Oct 23 '14 at 09:18
  • I'm not sure I understand? stdout gives me a message. I cut off every line except the two last ones since they aren't relevant, then print them. One of the lines say "Fatal error occured, no output pdf file produced!", so I assume that means it should generate an error message... You can see it here: [link](http://pastebin.com/rM0H2i8P) – user2875994 Oct 23 '14 at 09:29