From 8c164d19580938fe759ccb66a0010df885c04c03 Mon Sep 17 00:00:00 2001 From: zoe Date: Tue, 17 Sep 2024 11:44:25 +0200 Subject: [PATCH] help me chatgpt --- add_release_prefix.py | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/add_release_prefix.py b/add_release_prefix.py index f1b6746..02c3e13 100644 --- a/add_release_prefix.py +++ b/add_release_prefix.py @@ -1,12 +1,31 @@ import os import sys +import time directory = os.path.join("target", "bundled") -prefix = sys.argv[1] +prefix = sys.argv[1] # assuming prefix is passed as the first argument +max_retries = 5 # Number of retries for a locked file +delay = 2 # Delay in seconds between retries +# Iterate over files in the directory for filename in os.listdir(directory): - old_file = os.path.join(directory) - new_file = os.path.join(directory, prefix + filename) - os.rename(old_file, new_file) + old_file = os.path.join(directory, filename) # ensure you access the full file path + new_file = os.path.join(directory, prefix + filename) # add prefix to the filename + + retries = 0 + while retries < max_retries: + try: + os.rename(old_file, new_file) + print(f"Renamed: {old_file} -> {new_file}") + break + except PermissionError: + print(f"PermissionError: File '{old_file}' is being used. Retrying in {delay} seconds...") + retries += 1 + time.sleep(delay) # Wait before retrying + except Exception as e: + print(f"Error: {e}") + break + else: + print(f"Failed to rename '{old_file}' after {max_retries} attempts.") -print(f"Prefix '{prefix}' added to release files") +print(f"Prefix '{prefix}' added to release files.") \ No newline at end of file