How to use `subprocess` command with pipes

后端 未结 9 1571
春和景丽
春和景丽 2020-11-22 02:32

I want to use subprocess.check_output() with ps -A | grep \'process_name\'. I tried various solutions but so far nothing worked. Can someone guide

9条回答
  •  轮回少年
    2020-11-22 03:02

    After Python 3.5 you can also use:

        import subprocess
    
        f = open('test.txt', 'w')
        process = subprocess.run(['ls', '-la'], stdout=subprocess.PIPE, universal_newlines=True)
        f.write(process.stdout)
        f.close()
    

    The execution of the command is blocking and the output will be in process.stdout.

提交回复
热议问题