python subprocess output to list or file

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

    With python2.7 you can use subprocess.check_output:

    ls_lines = subprocess.check_output(['ls', '-l']).splitlines()
    

    Prior to python2.7, you need to use the lower level api, which is a bit more involved.

    ls_proc = subprocess.Popen(['ls', '-l'], stdout=subprocess.PIPE)
    ls_proc.wait()
    # check return code
    ls_lines = ls_proc.stdout.readlines()
    
    0 讨论(0)
  • 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

    0 讨论(0)
  • 2020-12-31 21:44

    If what you really want is to list a directory, rather use os.listdir

    import os
    files = os.listdir('/path/to/dir')
    for file in files:
        print(file)
    
    0 讨论(0)
  • 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

    0 讨论(0)
  • 2020-12-31 21:49

    One way to access to the information in ls -l output is to parse it. For example, csv.DictReader could be use to map each column to a field in a dictionary:

    import subprocess
    import csv
    
    process = subprocess.Popen(['ls', '-l'], stdout=subprocess.PIPE)
    stdout, stderr = process.communicate()
    
    reader = csv.DictReader(stdout.decode('ascii').splitlines(),
                            delimiter=' ', skipinitialspace=True,
                            fieldnames=['permissions', 'links',
                                        'owner', 'group', 'size',
                                        'date', 'time', 'name'])
    
    for row in reader:
        print(row)
    

    The code above will print a dictionary for each line in ls -l output such as:

    {'group': '<group_name>',
     'name': '<filename>',
     'links': '1',
     'date': '<modified_date>',
     'time': '<modified_time>',
     'owner': '<user_name>',
     'permissions': '-rw-rw-r--',
     'size': '<size>'}
    
    0 讨论(0)
提交回复
热议问题