I am working on a script that recursively searches a directory to find files which matches a regex 5 or more times.
Using the script I can search for files which look like
https://github.com/nicolargo/glances/issues/1087
https://askubuntu.com/questions/758696/cannot-login-into-locked-ubuntu-14-04-session-unity
https://github.com/restic/restic/issues/14
https://www.huffpost.com/entry/our-shared-fears-and-how-_b_776720?guccounter=1
https://www.collaborativefund.com/blog/ideas-that-changed-my-life/
https://www.livescience.com/52882-charitable-acts-lead-to-bad-behavior.html
This has 5 or more lines that start with https://
. If fewer than 5 lines start with https://
, the file name will not be printed.
So far, what I came up with is:
from pathlib import Path
import re
for path in Path('/tmp/').rglob('*.adoc'):
with open(path.resolve()) as file:
count = 0
for item in file:
if re.match("^https://", item):
count = count + 1
if count > 4:
print(path.resolve())
break
The code has two loops and two conditions and it does not look right to me. How can I improve the code?
item.startswith
\$\endgroup\$if re.match('(^https://.*){5}','\n'.join(file.readlines()),re.MULTILINE): print(path.resolve())
. That will probably be slower thanstartswith
, but it may be easier to read. \$\endgroup\$