Pregunta

I have the following code in python:

import subprocess
var = subprocess.Popen(["pdflatex","file.tex"])

Where var is a throw away variable because I don't care about the return variable. It does what I want (generates a pdf file from the latex file file.tex), but then waits for user input. These are the last lines of the program and I just want it to end without waiting for input. How can I do this?

¿Fue útil?

Solución

Redirect stdin to devnull, to avoid a subprocess to pause waiting for user input:

import os
from subprocess import Popen
try:
    from subprocess import DEVNULL # Python 3
except ImportError:
    DEVNULL = open(os.devnull, 'r+b', 0)

Popen(['pdflatex', 'file.tex'], stdin=DEVNULL, stdout=DEVNULL, stderr=DEVNULL)

I assume that you don't care about stdout/stderr also.

Otros consejos

If you're not concerned about the output, try this:

args = ["pdflatex","file.tex"]
var = subprocess.Popen(args, stdout=subprocess.PIPE)
trash = var.communicate() # dump the output of the program calls
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top