Python subprocess readlines()?

后端 未结 5 1576
小蘑菇
小蘑菇 2021-01-01 16:46

So I\'m trying to move away from os.popen to subprocess.popen as recommended by the user guide. The only trouble I\'m having is I can\'t seem to find a way of making readli

5条回答
  •  天涯浪人
    2021-01-01 17:30

    With subprocess.Popen, use communicate to read and write data:

    out, err = subprocess.Popen(['ls','-l'], stdout=subprocess.PIPE).communicate() 
    

    Then you can always split the string from the processes' stdout with splitlines().

    out = out.splitlines()
    

提交回复
热议问题