import os
import random

def find_shortest_filename(source_dir, result_file):
    # Проверяем есть ли папка result, если нет - создаем
    result_dir = os.path.dirname(result_file)
    if not os.path.exists(result_dir):
        os.makedirs(result_dir)
        print(f"Created directory {result_dir}")
    
    files = [f for f in os.listdir(source_dir) if os.path.isfile(os.path.join(source_dir, f))]
    
    if not files:
        print(f"No files found in {source_dir}")
        return
    
    # Выбор случайного файла
    random_file = random.choice(files)
    random_file_path = os.path.join(source_dir, random_file)
    
    # Копируем файл в result/data.txt., если файл уже есть перезаписываем
    with open(random_file_path, 'r') as f_in, open(result_file, 'w') as f_out:
        f_out.write(f_in.read())

    print(f"Moved {random_file} to {result_file}")

if __name__ == "__main__":
    find_shortest_filename('/var/data', '/var/result/data.txt')