How to just call a command and not get its output

user225312 picture user225312 · Feb 14, 2011 · Viewed 55.4k times · Source

In Python, what is the shortest and the standard way of calling a command through subprocess but not bothering with its output.

I tried subprocess.call however it seems to return the output. I am not bothered with that, I just need to run the program silently without the output cluttering up the screen.

If it helps, I am callling pdflatex and my intention is just to call it.

Answer

Sven Marnach picture Sven Marnach · Sep 19, 2012

In case your process produces significant amounts of output that you don't want to buffer in memory, you should redirect the output to the electronic trash can:

with open(os.devnull, "w") as f:
    subprocess.call(["pdflatex", filename], stdout=f)

The variable os.devnull is the name of the null device of your operating system (/dev/null on most OSes, nul on the other one).