【问题标题】:python subprocess and grep commandpython子进程和grep命令
【发布时间】:2015-10-12 05:25:29
【问题描述】:

我正在用 Python 编写一个脚本程序。在这个程序中,我需要获取文件名的 grep 结果并从中提取文件名。 我现在被'grep'困住了。 我尝试 grep 一个名为 NoMoreHotCorner_20151006.T 的文件并返回结果。 我的预期结果应该如下:

grep: command/CVS: Is a directory
grep: command/obsolete: Is a directory
command/yosemite-2015.K:#p system/NoMoreHotCorner_20151006.T 

我可以保证这个文件存在,我的路径没有问题。

我可以通过使用 os.system() 得到想要的结果。

>>> import os
>>> from subprocess import Popen, PIPE
>>> from subprocess import CalledProcessError, check_output
>>> string = 'grep '+'NoMoreHotCorner_20151006.T'+' command/*'
>>> os.system(string)
grep: command/CVS: Is a directory
grep: command/obsolete: Is a directory
command/yosemite-2015.K:#p system/NoMoreHotCorner_20151006.T 

但此函数无法将其输出返回到屏幕。所以我决定改用 subprocess.check_output 。但它引发了 CalledProcessError。

>>> p = check_output(string,shell=True)
grep: command/CVS: Is a directory
grep: command/obsolete: Is a directory
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 573, in check_output
    raise CalledProcessError(retcode, cmd, output=output)
subprocess.CalledProcessError: Command 'grep NoMoreHotCorner_20151006.T command/*' returned non-zero exit status 2

所以我在堆栈溢出时查找了类似的问题并处理了这次异常。但它只打印前两行....

>>> try:
...     p = check_output(string,shell=True)
... except CalledProcessError as e:
...     print(e.returncode)
... 
grep: command/CVS: Is a directory
grep: command/obsolete: Is a directory
2

所以我这次改用 Popen。但是,它仍然存在问题。在它输出前两行之后,它会永远运行。

>>> p = Popen(string,shell=True,stdout=PIPE)
>>> grep: command/CVS: Is a directory
grep: command/obsolete: Is a directory

KeyboardInterrupt

我完全被困在这一点上。

【问题讨论】:

    标签: python linux bash


    【解决方案1】:

    尝试将p = Popen(string,shell=True,stdout=PIPE) 更改为p = Popen(['grep', 'NoMoreHotCorner_20151006.T', 'command/*'],stdout=PIPE),然后使用p.communicate() 读取输出。

    或者,使用check_output(['grep', 'NoMoreHotCorner_20151006.T', 'command/*'])

    【讨论】:

    • 你的意思是p = Popen(['grep', 'NoMoreHotCorner_20151006.T', 'command/*'],stdout=PIPE)? command 的参数应该只占一个位置。它仍然不起作用。 &gt;&gt;&gt; p = Popen(['grep', 'NoMoreHotCorner_20151006.T', 'command/*'],stdout=PIPE) &gt;&gt;&gt; grep: command/*: No such file or directory
    • 找不到文件..&gt;&gt;&gt; try: ... check_output(['grep', 'NoMoreHotCorner_20151006.T', 'command/*']) ... except CalledProcessError as e: ... print(e.returncode) ... grep: command/*: No such file or directory 2
    • 是的,我打错了。 Args 应该在一个列表中。我刚刚验证并且两者都在我的机器上工作。对于grep 命令,command/* 是您要 grep 的模式,NoMoreHotCorner_20151006.T 是文件吗?模式需要在文件名之前。
    • 还是不行。如果我使用列表而不是字符串作为第一个参数,则甚至找不到该文件。字符串也应该根据关于 subprocess.check_output 的 python2.7 文档工作。
    【解决方案2】:

    此外,如果您需要将命令作为字符串添加到列表中

    导入 shlex 并使用 shlex.split(command) 获取您的列表

    【讨论】:

      猜你喜欢
      • 2015-12-19
      • 2021-12-10
      • 2011-12-18
      • 2018-07-03
      • 2017-01-09
      • 2020-11-26
      • 2012-10-31
      • 1970-01-01
      • 2017-11-22
      相关资源
      最近更新 更多