subprocess.run()
function只存在於Python 3.5及更新版本中。
,這很容易然而,反向移植:
def run(*popenargs, input=None, check=False, **kwargs):
if input is not None:
if 'stdin' in kwargs:
raise ValueError('stdin and input arguments may not both be used.')
kwargs['stdin'] = subprocess.PIPE
process = subprocess.Popen(*popenargs, **kwargs):
try:
stdout, stderr = process.communicate(input)
except:
process.kill()
process.wait()
raise
retcode = process.poll()
if check and retcode:
raise subprocess.CalledProcessError(
retcode, process.args, output=stdout, stderr=stderr)
return retcode, stdout, stderr
沒有爲超時的支持,併爲完成過程信息沒有自定義類,所以我只能返回retcode
,stdout
和stderr
信息。否則它會和原來的一樣。
'subprocess'不應該(並且不......)有一個叫做'run'的方法。 – DeepSpace
@DeepSpace它在Python 3中執行https://docs.python.org/3/library/subprocess.html#subprocess.run,但不幸的是,他們正在使用Python 2 –
@MosesKoledoye嗯,這個問題是用'python 2.7 ';) – DeepSpace