Optimize file handling in the find module (#38192)
* Optimize file handling Use the best practice of opening and doing operations on an opened file Signed-off-by: Daniel Andrei Minca <mandrei17@gmail.com> * Fix docstring to Sphinx type - update the docstrings to Sphinx type, as suggested by Toshio - Move the pattern object assignment outside the context manager, as suggested by Matt Signed-off-by: Daniel Andrei Minca <mandrei17@gmail.com>
This commit is contained in:
parent
450cfa8776
commit
89d6c36584
1 changed files with 14 additions and 9 deletions
|
@ -250,19 +250,24 @@ def sizefilter(st, size):
|
|||
|
||||
|
||||
def contentfilter(fsname, pattern):
|
||||
'''filter files which contain the given expression'''
|
||||
"""
|
||||
Filter files which contain the given expression
|
||||
:arg fsname: Filename to scan for lines matching a pattern
|
||||
:arg pattern: Pattern to look for inside of line
|
||||
:rtype: bool
|
||||
:returns: True if one of the lines in fsname matches the pattern. Otherwise False
|
||||
"""
|
||||
if pattern is None:
|
||||
return True
|
||||
|
||||
try:
|
||||
f = open(fsname)
|
||||
prog = re.compile(pattern)
|
||||
|
||||
try:
|
||||
with open(fsname) as f:
|
||||
for line in f:
|
||||
if prog.match(line):
|
||||
f.close()
|
||||
return True
|
||||
|
||||
f.close()
|
||||
except:
|
||||
pass
|
||||
|
||||
|
|
Loading…
Reference in a new issue