Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to just call a command and not get its output [duplicate]

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.

like image 826
user225312 Avatar asked Feb 14 '11 20:02

user225312


People also ask

How do you store output of subprocess call?

Popen(args,stdout = subprocess. PIPE). stdout ber = raw_input("search complete, display results?") print output #... and on to the selection process ... You now have the output of the command stored in the variable "output".

What is subprocess call ()?

Subprocess call(): Subprocess has a method call() which can be used to start a program. The parameter is a list of which the first argument must be the program name. The full definition is: subprocess.call(args, *, stdin=None, stdout=None, stderr=None, shell=False) # Run the command described by args.


Video Answer


2 Answers

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:

subprocess.run(["pdflatex", filename], stdout=subprocess.DEVNULL) 

The special value DEVNULL indicates that the output is sent to the appropriate null device of your operating system (/dev/null on most OSes, nul on the other one).

like image 176
Sven Marnach Avatar answered Oct 14 '22 14:10

Sven Marnach


p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) out, err = p.communicate() # do something with out, err, or don't bother altogether. 

Basically, this "pipes" whatever cmd outputs to stdout and stderr to in-memory buffers prepared by subprocess. What you do with the content of those buffers are up to you. You can examine them, or don't bother with them altogether. But the side effect of piping to these buffers is that they won't be printed to the terminal's.

edit: This also works with the convenience method, call. For a demonstration:

>>> subprocess.call('ping 127.0.0.1')  Pinging 127.0.0.1 with 32 bytes of data: Reply from 127.0.0.1: bytes=32 time<1ms TTL=128 Reply from 127.0.0.1: bytes=32 time<1ms TTL=128 Reply from 127.0.0.1: bytes=32 time<1ms TTL=128 Reply from 127.0.0.1: bytes=32 time<1ms TTL=128  Ping statistics for 127.0.0.1:     Packets: Sent = 4, Received = 4, Lost = 0 (0% loss), Approximate round trip times in milli-seconds:     Minimum = 0ms, Maximum = 0ms, Average = 0ms 0 >>> subprocess.call('ping 127.0.0.1', stdout=subprocess.PIPE) 0 

edit-2: A note of caution for subprocess.call:

Note: Do not use stdout=PIPE or stderr=PIPE with this function. As the pipes are not being read in the current process, the child process may block if it generates enough output to a pipe to fill up the OS pipe buffer.

like image 20
Santa Avatar answered Oct 14 '22 12:10

Santa