DAS_2024_1/haliullov_kamil_lab_2/oneapp/main.py
2024-12-29 11:43:21 +04:00

34 lines
1.0 KiB
Python

import os
import shutil
import random
def select_random_file(directory):
files = os.listdir(directory)
if not files:
raise FileNotFoundError("Каталог пустой")
random_file = random.choice(files)
return os.path.join(directory, random_file)
def copy_file_to_destination(source_file, destination_folder, new_filename="data.txt"):
destination_path = os.path.join(destination_folder, new_filename)
shutil.copy(source_file, destination_path)
def main():
input_directory = "/var/data"
output_directory = "/var/result"
try:
# Выбор случайного файла
random_file_path = select_random_file(input_directory)
# Копирование файла в целевой каталог
copy_file_to_destination(random_file_path, output_directory)
print(f"Копирован файл: {random_file_path} в {os.path.join(output_directory, 'data.txt')}")
except FileNotFoundError as e:
print(e)
if __name__ == "__main__":
main()