How to capture Python interpreter's and/or CMD.EXE's output from a Python script?

后端 未结 5 1664
感动是毒
感动是毒 2020-12-06 12:52
  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?
5条回答
  •  生来不讨喜
    2020-12-06 13:08

    In which context are you asking?

    Are you trying to capture the output from a program you start on the command line?

    if so, then this is how to execute it:

    somescript.py | your-capture-program-here
    

    and to read the output, just read from standard input.

    If, on the other hand, you're executing that script or cmd.exe or similar from within your program, and want to wait until the script/program has finished, and capture all its output, then you need to look at the library calls you use to start that external program, most likely there is a way to ask it to give you some way to read the output and wait for completion.

提交回复
热议问题