File size: 809 Bytes
1b420ad | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | import os
ROOT_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
TXT_LOGS_DIR = os.path.join(ROOT_DIR, "txt logs")
os.makedirs(TXT_LOGS_DIR, exist_ok=True)
for file_name in os.listdir(ROOT_DIR):
if file_name.endswith(".txt"):
dest_path = os.path.join(TXT_LOGS_DIR, file_name)
if os.path.exists(dest_path):
n = 1
file2 = file_name[:-4] + f"_{n}.txt"
dest_path = os.path.join(TXT_LOGS_DIR, file2)
while os.path.exists(dest_path):
n += 1
file2 = file_name[:-4] + f"_{n}.txt"
dest_path = os.path.join(TXT_LOGS_DIR, file2)
os.rename(os.path.join(ROOT_DIR, file_name), dest_path)
continue
os.rename(os.path.join(ROOT_DIR, file_name), dest_path)
|