python:运行外部程序并将输出直接定向到文件并等待完成 [英] python: run external program and direct output to file and wait for finish

查看:744
本文介绍了python:运行外部程序并将输出直接定向到文件并等待完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从python运行外部程序,将输出(大量文本)重定向到日志文件,然后等待该程序完成.我知道我可以通过bash做到这一点:

I want to run an external program from python, redirect output (lots of text) to a log file and wait for that program to finish. I know I can do it via bash:

#! /bin/bash
my_external_program > log_file 2>&1
echo "done"

但是我该如何使用python做同样的事情?请注意,使用bash命令,我可以在程序运行时检查log_file.我也希望在python中使用此属性.

But how can I do the same with python? Note that with the bash command, I can check the log_file while the program is running. I want this property in python as well.

推荐答案

您可以检查上面的链接,确实是类似的问题.从子进程或从os.popen使用Popen,可以检查实时.

You can check the link above, it is indeed similar issue. Using Popen from subprocess or from os.popen it is possible to check real time.

使用简单的os.system(您的脚本>/tmp/mickey.log")也将运行该脚本,但是它将在此之前等待命令的执行.

With a simple os.system ("your script > /tmp/mickey.log") will also run the script, but it will wait the execution of the command before.

请告诉我这是否可以解决您的问题.

Please let me know if this solve your issue.

这篇关于python:运行外部程序并将输出直接定向到文件并等待完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆