Python: how to suppress os.system output

In Python, if I use "wget" to upload a file using os.system ("wget"), it appears on the screen like this:

Resolving... Connecting to ... HTTP request sent, awaiting response... 100%[====================================================================================================================================================================>] 19,535,176 8.10M/s in 2.3s 

etc. on the screen.

What can I do to save this output in a file and not display it on the screen?

I am currently running the command as follows:

theurl = "<file location>"

downloadCmd = "wget" + theurl

os.system (downloadCmd)

+8
python
source share
4 answers

To answer your direct question, like the others, you should seriously consider using the subprocess module. Here is an example:

 from subprocess import Popen, PIPE, STDOUT wget = Popen(['/usr/bin/wget', theurl], stdout=PIPE, stderr=STDOUT) stdout, nothing = wget.communicate() with open('wget.log', 'w') as wgetlog: wgetlog.write(stdout) 

But you do not need to call the system to download the file, let python do the hard work for you.

Using urllib ,

 try: # python 2.x from urllib import urlretrieve except ImportError: # python 3.x from urllib.request import urlretrieve urlretrieve(theurl, local_filename) 

Or urllib2 ,

 import urllib2 response = urllib2.urlopen(theurl) with open(local_filename, 'w') as dl: dl.write(response.read()) 

local_filename is the destination path of your choice. Sometimes this can be determined automatically, but the approach depends on your circumstances.

+5
source share

The os.system functions run the command through the shell, so you can also redirect any stdio redirects there. You should also use the -q (silent) flag for wget.

 cmd = "wget -q " + theurl + " >/dev/null 2>&1" 

However, there are better ways to do this in python, such as the pycurl shell for libcurl or the "Stock" urllib2 .

+20
source share

As others have noted, you can use your own Python library modules to input I / O, or change the command line to redirect output.

But for complete control over the output, it is best to use the Python subprocess module instead of os.system() . Using subprocess will allow you to capture the output and verify it or pass arbitrary data to standard input.

If you need a quick and dirty way to start something, use os.system() . When you want to completely control how you start something, use subprocess .

+1
source share

The wget process simply writes STDOUT (and possibly STDERR if something goes wrong), and they are still “connected” to the terminal.

To stop this, redirect (or close) the specified file descriptors. Take a look at the subprocess module, which allows you to configure the specified file descriptors when the process starts. ( os.system just leaves the STDOUT / STDERR of the process just processed and therefore is inherited, but the subprocess module is more flexible.)

See Working with the Python Subprocess — Shells, Processes, Threads, Channels, Redirection, and more for many good examples and explanations (it introduces the concept of STDIN / STDOUT / STDERR and works from there).

There are probably better ways to handle this than using wget, but I will leave such answers.

Happy coding.

0
source share

All Articles