Created
January 5, 2023 13:03
-
-
Save softdream1981/c3e917869a2a272ec41b0771b75e2333 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import os | |
| import fnmatch | |
| import threading | |
| def search_files(directory, search_term): | |
| search_term = search_term.lower() | |
| for root, dirs, files in os.walk(directory): | |
| for file in fnmatch.filter(files, '*.txt') + fnmatch.filter(files, '*.doc') + fnmatch.filter(files, '*.docx') + fnmatch.filter(files, '*.pdf'): | |
| file_path = os.path.join(root, file) | |
| try: | |
| with open(file_path, 'r', encoding='utf-8') as f: | |
| file_contents = f.read().lower() | |
| if search_term in file_contents: | |
| print(f'Found {search_term} in {file_path}') | |
| except UnicodeDecodeError: | |
| # Skip files that cannot be decoded using utf-8 | |
| continue | |
| def search_in_thread(directory, search_term): | |
| t = threading.Thread(target=search_files, args=(directory, search_term)) | |
| t.start() | |
| t.join() | |
| current_directory = os.getcwd() | |
| search_term = input('Enter the string to search for: ') | |
| search_in_thread(current_directory, search_term) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment