reading a os.popen(command) into a string

前端 未结 5 1343
感情败类
感情败类 2020-12-28 17:39

I\'m not to sure if my title is right. What I\'m doing is writing a python script to automate some of my code writing. So I\'m parsing through a .h file. but I want to expan

相关标签:
5条回答
  • 2020-12-28 18:16

    The os.popen function just returns a file-like object. You can use it like so:

    import os
    
    process = os.popen('gcc -E myHeader.h')
    preprocessed = process.read()
    process.close()
    

    As others have said, you should be using subprocess.Popen. It's designed to be a safer version of os.popen. The Python docs have a section describing how to switch over.

    0 讨论(0)
  • 2020-12-28 18:19
    import subprocess
    
    p = subprocess.popen('gcc -E myHeader.h'.split(),
                         stdout=subprocess.PIPE)
    preprocessed, _ = p.communicate()
    

    String preprocessed now has the preprocessed source you require -- and you've used the "right" (modern) way to shell to a subprocess, rather than old not-so-liked-anymore os.popen.

    0 讨论(0)
  • 2020-12-28 18:19

    Here is another approach which captures both regular output plus error output:

    com_str = 'uname -a'
    command = subprocess.Popen([com_str], stdout=subprocess.PIPE, shell=True)
    (output, error) = command.communicate()
    print output
    
    Linux 3.11.0-20-generic  Fri May 2 21:32:55 UTC 2014 GNU/Linux
    

    and

    com_str = 'id'
    command = subprocess.Popen([com_str], stdout=subprocess.PIPE, shell=True)
    (output, error) = command.communicate()
    print output
    
    uid=1000(myname) gid=1000(myGID) groups=1000(mygrp),0(root)
    
    0 讨论(0)
  • 2020-12-28 18:26

    you should use subprocess.Popen() there are numerous examples on SO

    How to get output from subprocess.Popen()

    0 讨论(0)
  • 2020-12-28 18:29

    The os.popen() has been deprecated since Python 2.6. You should now use the subprocess module instead: http://docs.python.org/2/library/subprocess.html#subprocess.Popen

    import subprocess
    
    command = "gcc -E myHeader.h"  # the shell command
    process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=None, shell=True)
    
    #Launch the shell command:
    output = process.communicate()
    
    print output[0]
    

    In the Popen constructor, if shell is True, you should pass the command as a string rather than as a sequence. Otherwise, just split the command into a list:

    command = ["gcc", "-E", "myHeader.h"]  # the shell command
    process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=None)
    

    If you need to read also the standard error, into the Popen initialization, you can set stderr to subprocess.PIPE or to subprocess.STDOUT:

    import subprocess
    
    command = "gcc -E myHeader.h"  # the shell command
    process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
    
    #Launch the shell command:
    output, error = process.communicate()
    
    0 讨论(0)
提交回复
热议问题