在python中使用grep [英] Using grep in python
问题描述
有一个文件(query.txt),其中包含一些关键字/词组,这些关键字/词组可以使用grep与其他文件匹配.以下代码的最后三行运行正常,但是在while循环中使用同一命令时,它将进入无限循环或某种状态(即无响应).
There is a file (query.txt) which has some keywords/phrases which are to be matched with other files using grep. The last three lines of the following code are working perfectly but when the same command is used inside the while loop it goes into an infinite loop or something(ie doesn't respond).
import os
f=open('query.txt','r')
b=f.readline()
while b:
cmd='grep %s my2.txt'%b #my2 is the file in which we are looking for b
os.system(cmd)
b=f.readline()
f.close()
a='He is'
cmd='grep %s my2.txt'%a
os.system(cmd)
推荐答案
首先,您没有正确地遍历文件.您可以简单地使用for b in f:
而不使用.readline()
内容.
First of all, you are not iterating over the file properly. You can simply use for b in f:
without the .readline()
stuff.
然后,一旦文件名包含任何在外壳程序中具有特殊含义的字符,您的代码就会浮出水面.使用 subprocess.call
代替os.system()
并传递参数列表.
Then your code will blow in your face as soon as the filename contains any characters which have a special meaning in the shell. Use subprocess.call
instead of os.system()
and pass an argument list.
这是固定版本:
import os
import subprocess
with open('query.txt', 'r') as f:
for line in f:
line = line.rstrip() # remove trailing whitespace such as '\n'
subprocess.call(['/bin/grep', line, 'my2.txt'])
但是,通过完全不调用grep
,您可以进一步改善代码.
而是将my2.txt
读取为字符串,然后使用re
模块执行搜索.如果根本不需要正则表达式,甚至可以使用if line in my2_content
However, you can improve your code even more by not calling grep
at all.
Read my2.txt
to a string instead and then use the re
module to perform the search. In case you do not need a regex at all, you can even simply use if line in my2_content
这篇关于在python中使用grep的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!