How to run a subprocess with Python, wait for it to exit and get the full stdout as a string?

后端 未结 5 691
囚心锁ツ
囚心锁ツ 2020-12-23 12:00

So I noticed subprocess.call while it waits for the command to finish before proceeding with the python script, I have no way of getting the stdout, except with

5条回答
  •  感动是毒
    2020-12-23 12:38

    With Python 3.8 this workes for me. For instance to execute a python script within the venv:

        import subprocess
        import sys
        res = subprocess.run([
                  sys.executable,            # venv3.8/bin/python
                  'main.py', '--help',], 
                stdout=PIPE, 
                text=True)
        print(res.stdout)
    

提交回复
热议问题