繁体   English   中英

使用python的subprocesses模块运行shell命令

[英]run shell command using the subprocesses module of python

我正在使用Python 2.6。 检查以下链接之后: 从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 ,我得到了想要的结果。 您能给我解释一下可能的原因吗? 谢谢!

尝试在-w之后添加usr_eml:

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")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM