Skip to content

Instantly share code, notes, and snippets.

@ecatanzani
Created October 12, 2022 16:17
Show Gist options
  • Save ecatanzani/5c51d4e97012b353a5488ae708559ff5 to your computer and use it in GitHub Desktop.
Save ecatanzani/5c51d4e97012b353a5488ae708559ff5 to your computer and use it in GitHub Desktop.
utility to find files
import os
from glob import glob
import multiprocessing as mp
keywords = ['argparse']
main_folder = 'path_to_main_folder'
py_files = []
if os.path.isdir(main_folder):
py_files = [y for x in os.walk(main_folder) for y in glob(os.path.join(x[0], '*.py'))]
def search_in_file(file):
try:
with open(file, 'r') as f:
lines = [line.strip() for line in f.readlines()]
found_key = False
for keyword in keywords:
if any(keyword in s for s in lines) and not found_key:
print(file)
found_key = True
except:
pass
pool = mp.Pool(mp.cpu_count())
pool.apply_async(search_in_file, [(file) for file in py_files])
pool.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment