将批处理文件输出到Python脚本 [英] Piping Batch File output to a Python script

查看:149
本文介绍了将批处理文件输出到Python脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个Python脚本(在Windows中),该脚本运行一个批处理文件,并将该批处理文件的命令行输出作为输入.批处理文件运行我无权访问的进程,并根据这些进程是否成功提供输出.我想从批处理文件中获取这些消息,并在python脚本中使用它们.有人对如何执行此操作有任何想法吗?

I'm trying to write a python script (in windows) that runs a batch file and will take the command line output of that batch file as input. The batch file runs processes that I don't have access to and gives output based on whether those processes are successful. I'd like to take those messages from the batch file and use them in the python script. Anyone have any ideas on how to do this ?

推荐答案

import subprocess

output= subprocess.Popen(
    ("c:\\bin\\batch.bat", "an_argument", "another_argument"),
    stdout=subprocess.PIPE).stdout

for line in output:
    # do your work here

output.close()

请注意,最好使用"@echo off"启动批处理文件.

Note that it's preferable to start your batch file with "@echo off".

这篇关于将批处理文件输出到Python脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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