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)