如何将 .exe 的输出重定向到 python 中的文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/880918/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to redirect the output of .exe to a file in python?
提问by monkut
In a script , I want to run a .exe with some command line parameters as "-a",and then
在脚本中,我想运行一个带有一些命令行参数为“-a”的 .exe,然后
redirect the standard output of the program to a file?
将程序的标准输出重定向到文件?
How can I implement that?
我该如何实施?
回答by monkut
You can redirect directly to a file using subprocess.
您可以使用子进程直接重定向到文件。
import subprocess
with open('output.txt', 'w') as output_f:
p = subprocess.Popen('Text/to/execute with-arg',
stdout=output_f,
stderr=output_f)
回答by Alex Martelli
Easiest is os.system("the.exe -a >thefile.txt")
, but there are many other ways, for example with the subprocess
module in the standard library.
最简单的是os.system("the.exe -a >thefile.txt")
,但还有许多其他方法,例如使用subprocess
标准库中的模块。
回答by Anurag Uniyal
You can do something like this e.g. to read output of ls -l (or any other command)
你可以做这样的事情,例如读取 ls -l (或任何其他命令)的输出
p = subprocess.Popen(["ls","-l"],stdout=subprocess.PIPE)
print p.stdout.read() # or put it in a file
you can do similar thing for stderr/stdin
你可以为 stderr/stdin 做类似的事情
but as Alex mentioned if you just want it in a file, just redirect the cmd output to a file
但正如亚历克斯提到的,如果你只想要一个文件,只需将 cmd 输出重定向到一个文件
回答by mavnn
If you just want to run the executable and wait for the results, Anurag's solution is probably the best. I needed to respond to each line of output as it arrived, and found the following worked:
如果你只想运行可执行文件并等待结果,Anurag 的解决方案可能是最好的。我需要在每一行输出到达时对其做出响应,并发现以下方法有效:
1) Create an object with a write(text) method. Redirect stdout to it (sys.stdout = obj). In your write method, deal with the output as it arrives.
1) 使用 write(text) 方法创建一个对象。将标准输出重定向到它(sys.stdout = obj)。在您的 write 方法中,在输出到达时对其进行处理。
2) Run a method in a seperate thread with something like the following code:
2)在一个单独的线程中运行一个方法,类似于以下代码:
p = subprocess.Popen('Text/to/execute with-arg', stdout=subprocess.PIPE,
stderr=subprocess.PIPE, shell=False)
while p.poll() is None:
print p.stdout.readline().strip()
Because you've redirected stdout, PIPE will send the output to your write method line by line. If you're not certain you're going to get line breaks, read(amount) works too, I believe.
由于您已重定向标准输出,PIPE 将逐行将输出发送到您的写入方法。如果您不确定是否会换行,我相信 read(amount) 也可以。
3) Remember to redirect stdout back to the default: sys.stdout = __sys.stdout__
3)记住将标准输出重定向回默认值: sys.stdout = __sys.stdout__
回答by kakyo
Although the title (.exe) sounds like it's a problem on Windows. I had to share that the accepted answer (subprocess.Popen() with stdout/stderr arguments) didn't work for me on Mac OS X (10.8) with python 2.7.
尽管标题 (.exe) 听起来像是 Windows 上的问题。我不得不分享接受的答案(带有 stdout/stderr 参数的 subprocess.Popen())在带有 python 2.7 的 Mac OS X (10.8) 上对我不起作用。
I had to use subprocess.check_output() (python 2.7 and above) to make it work. Example:
我必须使用 subprocess.check_output() (python 2.7 及更高版本)才能使其工作。例子:
import subprocess
cmd = 'ls -l'
out = subprocess.check_output(cmd, shell=True)
with open('my.log', 'w') as f:
f.writelines(out)
f.close()
Note that this solution writes all the accumulated output out when the program finishes. If you want to monitor the log file during the run. You may want to try something else. In my own case, I only cared about the end result.
请注意,此解决方案会在程序完成时写出所有累积的输出。如果要在运行期间监视日志文件。您可能想尝试其他方法。就我自己而言,我只关心最终结果。