Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop subprocess.Popen waiting for user input in python

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?

like image 405
Frank Anthony Avatar asked Jan 11 '23 16:01

Frank Anthony


1 Answers

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.

like image 82
jfs Avatar answered Jan 31 '23 04:01

jfs