DAS_2024_1/melnikov_igor_lab_2/generate_files.py
2024-12-15 16:56:11 +04:00

32 lines
1.1 KiB
Python

import os
import random
import string
def generate_random_filename(length):
return ''.join(random.choices(string.ascii_lowercase + string.digits, k=length)) + '.txt'
def generate_data_files(directory, num_files, min_lines, max_lines):
# Создание директории, если таковой не существует
if not os.path.exists(directory):
os.makedirs(directory)
# Проверка, пустая ли директория
if os.listdir(directory):
print(f"{directory} is not empty, skipping file generation.")
return
# Генерация файлов
for _ in range(num_files):
file_name = generate_random_filename(random.randint(1, 20))
file_path = os.path.join(directory, file_name)
with open(file_path, 'w') as f:
num_lines = random.randint(min_lines, max_lines)
for _ in range(num_lines):
f.write(f"{random.randint(1, 1000)}\n")
print(f"Generated file: {file_path}")
if __name__ == "__main__":
generate_data_files('/var/data', num_files=random.randint(5, 15), min_lines=1, max_lines=50)