Python - 带有引号和管道 Grep 的子进程 [英] Python - subprocess with Quotes and Pipe Grep

查看:34
本文介绍了Python - 带有引号和管道 Grep 的子进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试将简单的 grep 命令导入 python 时遇到问题.我想在文件或列表中获取以下命令的输出.

im having an issue trying to get a simple grep command into python. I want to take the output of the following command in a file or a list.

grep -c 'some thing' /home/user/* | grep -v :0

这就是我所拥有的,但它根本不起作用......

This is what I have, but its not working at all...

thing = str(subprocess.Popen(['grep', '-c', 'some thing', '/home/user/*', '|', 'grep', '-v', ':0'], stdout=subprocess.PIPE)

基本上,我需要搜索目录中的文件,如果目录中的任何文件中缺少我的字符串,则返回结果.

Basically I need to search files in a directory and return a result if my string is missing from any of the files in the directory.

工作代码(谢谢!!):

Working Code (Thanks!!):

thing = subprocess.Popen(('grep -c "some thing" /home/user/* | grep -v ":0"' ),shell=True, stdout=subprocess.PIPE)

推荐答案

管道 | 是一个 shell 功能.您必须使用带有 shell=True 的 Popen 才能使用它.

The pipe | is a shell feature. You have to use Popen with shell=True to use it.

这篇关于Python - 带有引号和管道 Grep 的子进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆