subprocess.check_output(),zgrep和匹配限制

时间:2016-09-29 17:58:51

标签: python bash subprocess zgrep

上下文:我试图找到python包的github存储库。要做到这一点,我为zithp提供了针对github网址的包存档。它工作正常,直到我限制输出1结果:

# works, returns a lot of results
subprocess.check_output(["zgrep", "-oha", "github", 'Django-1.10.1.tgz'])  #  works, a lot of results
# add -m1 to limit output, returns status 2 (doesn't work)
subprocess.check_output(["zgrep", "-m1", "-oha", "github", 'Django-1.10.1.tgz'])  #  works, a lot of results
# same command, different file - works
subprocess.check_output(["zgrep", "-m1", "-oha", "github", 'grabber.py'])

从命令行,所有三个命令都可以正常工作。有什么想法吗?

回溯:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/subprocess.py", line 574, in check_output
    raise CalledProcessError(retcode, cmd, output=output)
subprocess.CalledProcessError: Command '['zgrep', '-m1', '-oha', 'github', 'pkgs/Django-1.10.1.tar.gz']' returned non-zero exit status 2

命令行:

$ zgrep -m1 -oha "github.com/[^/]\+/django" pkgs/Django-1.10.1.tar.gz
github.com/django/django

1 个答案:

答案 0 :(得分:0)

因此,原因是:zgrep是一个shell脚本,它只是通过gzip和egrep管理存档。如果我们限制结果数量,egrep终止管道,所以gzip退出并投诉。在控制台中我们从未看到它,但是子进程以某种方式捕获此信号并引发异常。

解决方案:编写不会抱怨的迷你版zgrep

gunzip < $FILE 2> /dev/null | egrep -m1 -ohia $PATTERN