要回答您的直接问题,并且正如其他人所提到的那样,您应该强烈考虑使用
subprocess模块.这是一个例子:
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)
但是,没有必要呼叫系统下载文件,让python为你做繁重的工作.
try:
# python 2.x
from urllib import urlretrieve
except ImportError:
# python 3.x
from urllib.request import urlretrieve
urlretrieve(theurl, local_filename)
import urllib2
response = urllib2.urlopen(theurl)
with open(local_filename, 'w') as dl:
dl.write(response.read())
local_filename是您选择的目标路径.有时可以自动确定此值,但方法取决于您的情况.