2016-11-07 76 views
0

我想通過subprocess.call()函數執行unix/linux實用程序,並將命令的輸出存儲在變量中以便操作並在程序的其他部分分析命令的輸出。正在考慮的是將輸出重定向到文本文件,然後打開文本文件並遍歷文件的每一行並將數據輸入(存儲)到列表中。舉個例子:如何在Python中將Unix shell參數的輸出分配給變量

#! /usr/bin/python 

from subprocess import call 

# This listHome.py program has been designed and created as a 
# demonstration 
# author:oOpSgEoW 

class LstHome: 

    def lsthome(self): 
     # create the argument that will be passed to the call function 
     lsthme = 'ls $HOME > HomeList.txt' 
     # call the function 
     call(lsthme, shell=True) 

    def add_both(self): 

     # create a list and a file object for the file 
     lstOne = [] 
     fila = open('HomeList.txt', 'r') 

     # iterate over each line of the file and store the 
     # data into the each index of the list 
     for line in fila: 
      a = line.strip("\n") 
      lstOne.append(a) 

     # close the file 
     fila.close() 

     # return the list 
     return lstOne 

class HomePrint(): 

    # intialize the class, pass the list as lstAlpha 
    def __init__(self, lstAlpha=None): 
     # to keep the bounds of the list, which will initialize 
     # the list an empty list before the content of the list 
     # being passed as an argument 
     if lstAlpha is None: 
      lstTwo = [] 
     self.lstTwo = lstAlpha 
    def print_lst(self): 
     for line1 in self.lstTwo: 
      print(line1) 

def main(): 

    # create an object out of the first class 
    x = LstHome() 

    # call the lsthome() function in 
    # order to execute the command givenper 
    x.lsthome() 

    # assign and create an object out of the HomePrint class 
    # pass the output of the add_both() function from 
    # the LstHome() class 
    y = HomePrint(x.add_both()) 
    y.print_lst() 

    # an exit statement to the user 
    print 'The $HOME directory of the user has been printed\ndone.' 

main() 

有沒有一種方法可以讓我在我的第一類的功能分配call(lsthme, shell=True),而不是輸出重定向到文件HomeList.txt?所以基本上我問我可以/我可以代碼:

lsthme = 'ls $HOME' 
holdVar = call(lsthme, shell=True) 
print(holdVar) 

上述是一個合法的論點?如果不是什麼會產生類似的結果,看起來我試圖做什麼?

感謝


編輯:更正例如,對於其他有需要的主題Python的

#! /usr/bin/python 

from subprocess import PIPE, Popen, call 

# This listHome.py program has been designed and created to 
# demonstrate a multi-class program that has a class receive 
# an array/list as a parameter, and demonstrates interacting with a 
# Unix shell with a multi-class program 
# author:oOpSgEoW 

class LstHome: 

    def lsthome(self): 
     # create the argument that will be passed to the call function 
     # Use the Popen function of subprocess 
     lsthme = Popen("ls $HOME", shell=True, stdout=PIPE) 

     # assign the function to lstOne 
     lstOne = lsthme.stdout.read().split('\n') 
     # now that the data has been stored, the Pipe must be closed 
     # NOTE: Generally speaking, what goes up must come down. What lives, must die. What opens must eventually close. 
     lsthme.stdout.close() 

     # return the lstOne object. 
     return lstOne 

class HomePrint(): 

    # intialize the class, pass the list as lstAlpha 
    def __init__(self, lstAlpha=None): 
     # to keep the bounds of the list, which will initialize 
     # the list an empty list before the content of the list 
     # being passed as an argument 
     if lstAlpha is None: 
      lstTwo = [] 
     self.lstTwo = lstAlpha 

    def print_lst(self): 
     for line1 in self.lstTwo: 
     # NEVER PASS A NEWLINE RETURN TO THE CALL FUNCTION, 
     # AT THE END OF AN ARGUMENT, just in case you wanted to 
     # to take the output, or some of the output, and use as a 
     # command line input. For example: 
     # if ".py" in line1: 
     # line2 = line1.strip('\n') 
     # mover = 'mv ' 
     # newmov = ' $HOME/Documents/Examples_In_Py/' 
     # doTheMov = mover + line2 + newmov 
     # call(doTheMov, shell=True) 
     print(line1) 

def main(): 

    # create objects by performing class and functional abstraction 
    x = LstHome() 
    x.lsthome() 
    # pass the list as an argument 
    y = HomePrint(x.lsthome()) 
    y.print_lst() 

    print 'The $HOME directory of the user has been printed\ndone.' 

main() 

回答

3

你可能會替換爲Popen call方法!

你的代碼將着眼於年底somenthing這樣的:

from subprocess import PIPE, Popen 

res = Popen("ls $HOME",shell=True, stdout=PIPE) 
home_list = res.stdout.read().split('\n') 

,你將有主文件夾

+0

PIPE是否自動關閉?舉例來說,在賦值給home_list var – oOpSgEo

+0

之後,需要類似於以下語句的'stdout.close()'語句。在文檔中沒有指定類似的東西。請記住,它不是一個文件,它是標準流的副本,所以它的行爲就像一個緩衝區,在你使用它之後(讀到EOF)緩衝區將是空的 – Rollback

0

名單在Python 3.5,你可以簡單地使用subprocess.run

import subprocess 
output_bytes = subprocess.run("dir", shell=True, stdout=subprocess.PIPE).stdout 
+0

我是老生常談(我的首選)現在,只需2.6+。其實現在,在這裏,在過去的幾個月中,我一直在使用以下代碼: ':〜] $ python --version',其輸出結果如下: 'Python 2.7.10.0' – oOpSgEo

相關問題