2017-06-14 123 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