import os import sys def append_to_folder_name(folder_path, suffix): try: # Get the base folder name base_folder_name = os.path.basename(folder_path) # Append the suffix to the folder name new_folder_name = f"{base_folder_name}[{suffix}]" # Rename the folder os.rename(folder_path, os.path.join(os.path.dirname(folder_path), new_folder_name)) print(f"Folder renamed to: {new_folder_name}") except FileNotFoundError: print(f"Error: Folder '{folder_path}' not found.") def count_folder_files(folder_path): try: # Count the number of files in the folder file_count = sum(1 for entry in os.scandir(folder_path) if entry.is_file()) print(f"Number of files in the folder: {file_count}") return file_count except FileNotFoundError: print(f"Error: Folder '{folder_path}' not found.") def check_directory_exists(directory_name): return os.path.isdir(directory_name) def main(): if len(sys.argv) != 2: print("Usage: python script.py ") sys.exit(1) directory_name = sys.argv[1] suffix = count_folder_files(directory_name) # If [num] already exists at the end of the string, remove it so we can update it # Find the last occurrence of backslash last_backslash_index = directory_name.rfind('\\') # Find the first occurrence of '[' after the last backslash last_open_bracket_index = directory_name.find('[', last_backslash_index) # If '[' exists after the last backslash, remove everything after it if last_open_bracket_index != -1: directory_name_cleaned = directory_name[:last_open_bracket_index] else: # If '[' doesn't exist, keep the original string directory_name_cleaned = directory_name if check_directory_exists(directory_name): print(f"Directory '{directory_name}' exists.") #If folder was counted already, remove the old count. Ex: dirname[1] -> dirname os.rename(directory_name, os.path.join(os.path.dirname(directory_name), directory_name_cleaned)) directory_name = directory_name_cleaned append_to_folder_name(directory_name, suffix) else: print(f"Directory '{directory_name}' does not exist.") if __name__ == "__main__": main()