Загрузка данных


import os
import random
import time
folder_path = r'C:\\pygames\\Игра\\Things\\RU\\'

files = os.listdir(folder_path)

txt_files = [f for f in files if f.endswith ('.txt')]

def check_word_in_file(txtfile):
    file_name = folder_path + txtfile
    search_word = input("Введите слово для поиска: ").strip()

    try:
        with open(file_name, 'r', encoding='utf-8') as file:
            for line_number, line in enumerate(file, 1):
                if search_word in line:
                    print(f"Слово '{search_word}' найдено в строке {line_number}: {line.strip()}")
                    return

        print(f"Слово '{search_word}' не найдено в файле {file_name}.")

    except FileNotFoundError:
        print(f"Ошибка: Файл '{file_name}' не найден. Проверьте правильность пути.")
    except Exception as e:
        print(f"Произошла ошибка: {e}")


if txt_files:
    random_file = random.choice(txt_files)
    file_path = os.path.join(folder_path, random_file)
    
    print(f'{random_file}\n' + '-'*30)
    check_word_in_file(random_file)