如何从Python脚本捕获Python解释器和/或CMD.EXE的输出? [英] How to capture Python interpreter's and/or CMD.EXE's output from a Python script?

查看:381
本文介绍了如何从Python脚本捕获Python解释器和/或CMD.EXE的输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  • 是否可以从Python脚本捕获Windows CMD的输出?
  • >

    如果是这样,我应该查看哪个库?

    解决方案

    如果你正在谈论的python解释器或CMD.exe是你的脚本的父,然后没有,这是不可能的。在每个类似POSIX的系统(现在你运行的Windows,似乎,这可能有一些怪癖我不知道,YMMV)每个进程有三个流,标准输入,标准输出和标准错误。在默认情况下(当在控制台中运行时),这些指向控制台,但可以使用管道符号重定向:

      python script_a.py | python script_b.py 

    这将脚本a的标准输出流绑定到脚本B的标准输入流。在此示例中,标准错误仍然出现在控制台。请参阅维基百科上的标准讯息串相关文章。



    如果你正在谈论一个子进程,你可以从python启动它(stdin也是一个选项,如果你想双向通信):

      import subprocess 
    #当然你可以打开除python以外的东西:)
    process = subprocess.Popen([python,main.py] ,stdout = subprocess.PIPE,stderr = subprocess.PIPE)
    x = process.stderr.readline()
    y = process.stdout.readline()
    process.wait()

    查看Python subprocess 模块,以获取有关管理流程的信息。对于通信,process.stdin和process.stdout管道被视为标准文件对象



    用于管道,从标准输入读取 lassevk 建议你这样做:

      import sys 
    x = sys.stderr.readline()
    y = sys.stdin.readline()
    pre>

    sys.stdin和sys.stdout是如上所述的标准文件对象,定义在 sys 模块。您还可以查看管道模块。



    使用readline()读取数据是一个很简单的获取数据的方式。如果输出不是面向行或不确定的,您可能需要查看轮询这不幸的是在Windows中不工作,但我相信有一些替代品在那里。


    1. Is it possible to capture Python interpreter's output from a Python script?
    2. Is it possible to capture Windows CMD's output from a Python script?

    If so, which librar(y|ies) should I look into?

    解决方案

    If you are talking about the python interpreter or CMD.exe that is the 'parent' of your script then no, it isn't possible. In every POSIX-like system (now you're running Windows, it seems, and that might have some quirk I don't know about, YMMV) each process has three streams, standard input, standard output and standard error. Bu default (when running in a console) these are directed to the console, but redirection is possible using the pipe notation:

    python script_a.py | python script_b.py
    

    This ties the standard output stream of script a to the standard input stream of script B. Standard error still goes to the console in this example. See the article on standard streams on Wikipedia.

    If you're talking about a child process, you can launch it from python like so (stdin is also an option if you want two way communication):

    import subprocess
    # Of course you can open things other than python here :)
    process = subprocess.Popen(["python", "main.py"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    x = process.stderr.readline()
    y = process.stdout.readline()
    process.wait()
    

    See the Python subprocess module for information on managing the process. For communication, the process.stdin and process.stdout pipes are considered standard file objects.

    For use with pipes, reading from standard input as lassevk suggested you'd do something like this:

    import sys
    x = sys.stderr.readline()
    y = sys.stdin.readline()
    

    sys.stdin and sys.stdout are standard file objects as noted above, defined in the sys module. You might also want to take a look at the pipes module.

    Reading data with readline() as in my example is a pretty naïve way of getting data though. If the output is not line-oriented or indeterministic you probably want to look into polling which unfortunately does not work in windows, but I'm sure there's some alternative out there.

    这篇关于如何从Python脚本捕获Python解释器和/或CMD.EXE的输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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