如何捕获“subprocess.call"的输出?到一个文件? [英] How to capture the output from "subprocess.call" to a file?
本文介绍了如何捕获“subprocess.call"的输出?到一个文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的代码中,我有一行类似于:
In my code I have a line similar to this:
rval = subprocess.call(["mkdir",directoryName], shell=True)
我可以检查rval
,看看它是0
还是1
,但如果是1
,我想要来自命令 "A subdirectory or file ben already exists."
的文本文件格式,所以如果我想确保文本是,我可以将它与另一个文件进行比较一样.
and I can check rval
to see if it is 0
or 1
, but if it is 1
, I would like to have the text from the command "A subdirectory or file ben already exists."
in a file format, so I can compare it to another file if I want to make sure the text is the same.
是否可以有这样的一行,但我知道这行不通
Is it possible to have a line like this, but I know this does not work
rval = subprocess.call(["mkdir",directoryName], shell=True) >> filename
所以无论命令发生什么,文本都被捕获在filename
中,而rval
还有返回码?
so no matter what happens with the command, the text is captured in filename
, and rval
still has the return code?
推荐答案
import subprocess
f = open(r'c:\temp\temp.txt','w')
subprocess.call(['dir', r'c:\temp'], shell=True, stdout=f)
f.close()
这篇关于如何捕获“subprocess.call"的输出?到一个文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文