2024-09-23 15:28:34 +04:00
|
|
|
import random
|
|
|
|
import os
|
|
|
|
|
2024-09-23 16:00:41 +04:00
|
|
|
folder_path = '/data'
|
2024-09-23 15:28:34 +04:00
|
|
|
|
|
|
|
def creat_folder():
|
|
|
|
if not os.path.exists(folder_path):
|
|
|
|
os.makedirs(folder_path)
|
2024-09-23 16:00:41 +04:00
|
|
|
print(f"Папка `{folder_path}` создана!")
|
2024-09-23 15:28:34 +04:00
|
|
|
else:
|
|
|
|
print(f"Папка `{folder_path}` уже существует!")
|
|
|
|
|
|
|
|
def delete_files_in_folder():
|
|
|
|
for filename in os.listdir(folder_path):
|
|
|
|
file_path = os.path.join(folder_path, filename)
|
|
|
|
try:
|
|
|
|
if os.path.isfile(file_path):
|
|
|
|
os.remove(file_path)
|
2024-09-23 16:00:41 +04:00
|
|
|
print(f"Файл `{file_path}` удален.")
|
2024-09-23 15:28:34 +04:00
|
|
|
except Exception as e:
|
|
|
|
print(f'Ошибка при удалении файла {file_path}. {e}')
|
|
|
|
|
|
|
|
def creat_file():
|
|
|
|
for i in range(random.randrange(10, 20)):
|
|
|
|
file = open(f'{folder_path}/file_{i}.txt', 'w+')
|
2024-09-23 16:00:41 +04:00
|
|
|
print(f"Создан файл: {folder_path}/file_{i}.txt")
|
2024-09-23 15:28:34 +04:00
|
|
|
for i in range(random.randrange(10, 50)):
|
|
|
|
file.write(f'{create_lines()} \n')
|
|
|
|
file.close()
|
|
|
|
|
|
|
|
def create_lines():
|
2024-09-23 16:00:41 +04:00
|
|
|
lines = [i for i in range(random.randrange(1, 50), random.randrange(50, 100), random.randrange(1, 10))]
|
2024-09-23 15:28:34 +04:00
|
|
|
return lines
|
|
|
|
|
|
|
|
if os.path.exists(folder_path):
|
2024-09-23 16:00:41 +04:00
|
|
|
print(f"Папка `{folder_path}` существует, удаляем старые файлы...")
|
2024-09-23 15:28:34 +04:00
|
|
|
delete_files_in_folder()
|
|
|
|
creat_file()
|
|
|
|
else:
|
2024-09-23 16:00:41 +04:00
|
|
|
print(f"Папка `{folder_path}` не существует, создаем...")
|
2024-09-23 15:28:34 +04:00
|
|
|
creat_folder()
|
2024-09-23 16:00:41 +04:00
|
|
|
creat_file()
|
|
|
|
|
|
|
|
print("Скрипт на создание файлов выполнен.")
|