python 启动外部程序_python:运行外部程序并直接输出到文件并等待完成

本文介绍了如何在Python中实现类似Bash脚本的功能,即运行外部程序并将输出重定向到日志文件,并能实时检查日志内容。通过使用`subprocess`模块的`Popen`,可以实现程序的异步执行并实时获取输出。
摘要由CSDN通过智能技术生成

1586010002-jmsa.png

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"

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.

解决方案

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.

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值