2017-06-14 80 views
0

我正在使用Python 2.6。檢查下面的鏈接後: Running shell command from Python and capturing the output 這裏是我打算做的代碼:使用Python的子進程模塊運行shell命令

import subprocess 

    # input: user email account 
    # output: mailing list which contain the user as an owner 
    def list_owners (usr_eml): 
     p = subprocess.Popen(['/usr/lib/mailman/bin/find_member','-w'], 
          stdout=subprocess.PIPE, 
          stderr=subprocess.PIPE, 
          stdin=subprocess.PIPE) 
     out, err = p.communicate(usr_eml) 
     print out 
     m_list_usr_on = out.split() 

     print m_list_usr_on 
    list_owners("my_email") 

這段代碼的輸出是乾脆爲空。另一方面,如果我直接從shell命令運行代碼

/usr/lib/mailman/bin/find_member -w my_email ,我得到了期望的結果。 你能向我解釋一下可能的原因嗎? 謝謝!

回答

2

嘗試添加usr_eml的-w後:

import subprocess 

# input: user email account 
# output: mailing list which contain the user as an owner 
def list_owners (usr_eml): 
    p = subprocess.Popen(['/usr/lib/mailman/bin/find_member','-w',usr_eml], 
         stdout=subprocess.PIPE, 
         stderr=subprocess.PIPE, 
         stdin=subprocess.PIPE) 
    out, err = p.communicate() 
    print out 
    m_list_usr_on = out.split() 

    print m_list_usr_on 
list_owners("my_email") 
+0

它的工作原理很完美。 – highsky

相關問題