使用python的子进程模块运行shell命令

2024-05-15 19:48:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用Python 2.6。检查以下链接后: Running shell command from Python and capturing the output 以下是我计划执行的代码:

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

这段代码的输出是空的。 另一方面,如果我运行代码

/usr/lib/mailman/bin/find_member -w my_email 直接从shell命令,我得到了想要的结果。 你能给我解释一下可能的原因吗? 谢谢大家!


Tags: the代码mailmanoutputemaillibusrshell
1条回答
网友
1楼 · 发布于 2024-05-15 19:48:01

尝试在-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")

相关问题 更多 >