python subprocess output to list or file

前端 未结 5 1450
鱼传尺愫
鱼传尺愫 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:32

    from subprocess import Popen, PIPE
    output = Popen(['ls', '-l'], stdout=PIPE).communicate()[0]
    

    You can then do whatever you want with the output. See python docs for detailed documentation

提交回复
热议问题