python subprocess output to list or file

前端 未结 5 1423
鱼传尺愫
鱼传尺愫 2020-12-31 21:20

I want to run the following bash command in Python 3:

ls -l

I know that I can do the following:

from subprocess import call         


        
5条回答
  •  别那么骄傲
    2020-12-31 21:44

    Read about Popen. the set you asked for you get with

    import subprocess
    proc = subprocess.Popen(['ls','-l'],stdout=subprocess.PIPE,stderr=subprocess.PIPE)
    myset=set(proc.stdout)
    

    or do something like

    for x in proc.stdout : print x
    

    and the same for stderr

    you can examine the state of the process with

    proc.poll() 
    

    or wait for it to terminate with

    proc.wait()
    

    also read

    read subprocess stdout line by line

提交回复
热议问题