python Popen 获取输出,等待运行完成

import subprocess

def excuteCommand(com):
    ex = subprocess.Popen(com, stdout=subprocess.PIPE, shell=True)
    out, err  = ex.communicate()
    status = ex.wait()
    print("cmd in:", com)
    print("cmd out: ", out.decode())
    return out.decode()
  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Python的`popen`是一个用于执行shell命令并获取输出的函数。它允许你在Python脚本中执行外部命令,并获取命令的输出。`popen`函数返回一个打开的文件对象,你可以通过读取该文件对象来获取命令的输出。 在上述代码示例中,`popen`函数被用于执行shell命令`ls -la`并获取当前目录下的文件列表。命令的输出被保存在`result_list`变量中。你可以通过读取`result_list`来获取命令的输出。 此外,还提供了一些常用的方法来处理命令的输出。例如,可以使用正则匹配或字符过滤等方式对获取的数据进行解析,以获得需要的部分值。还可以定义自己的方法来处理输出,如在shell终端每行数据中查找固定的字符并返回行号的方法`shellfindstrline`。 综上所述,`python popen`是一个用于执行shell命令并获取输出的函数,在处理输出时可以使用各种方法进行解析和处理。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [python Popen 获取输出,等待运行完成示例](https://download.csdn.net/download/weixin_38717359/12856780)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [python-popen函数解析](https://blog.csdn.net/qq_45689245/article/details/126392739)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值