Subprocess.call& check_output Python2.7

时间:2014-01-05 00:21:27

标签: python subprocess

我正在阅读有关子进程模块的内容并且非常困惑。在我的搜索功能下,我想使用subprocess.check_output在grep无法在文本文件中找到用户输入时返回我选择的打印错误消息。我该怎么做呢?使用Python 2.7

import subprocess

def menu():
    usr = raw_input("Press 1 if you are creating a new entry, press 2 for search or 3 to exit")
    if usr == '1':
        collect()
    if usr == '2':
        search()
    if usr == '3':
        sys.exit()
    if usr == '4':
        subprocess.call(['vim', '-c', '10', 'book.txt'])
def search():
    inp = raw_input("Please enter a name:")
    subprocess.call(['rgrep', '-e', inp])
    search()
def collect():
    def do_global():
        global name, ac, number
        name = raw_input("Name")
        ac = raw_input("Area Code")

1 个答案:

答案 0 :(得分:1)

if subprocess.call(['rgrep', '-e', inp]) != 0: # rgrep failed
   print("error message of your choosing")

要了解grep可能产生的退出状态,请参阅its man page