From 188dd1c57bd6bd4f493177af1de89ec74102607d Mon Sep 17 00:00:00 2001 From: Aroy-Art Date: Wed, 20 Dec 2023 21:36:34 +0100 Subject: [PATCH] Add: Code comments --- PythonScripts/rename_backslash.py | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/PythonScripts/rename_backslash.py b/PythonScripts/rename_backslash.py index 2c5d746..bc068d4 100644 --- a/PythonScripts/rename_backslash.py +++ b/PythonScripts/rename_backslash.py @@ -1,22 +1,40 @@ import os import sys +# Function to replace backslashes in filenames within a directory def replace_backslash(directory): + # Traverse through the directory and its subdirectories for root, _, files in os.walk(directory): for filename in files: + # Check if the filename contains a backslash if "\\" in filename: + # Construct the full file path file_path = os.path.join(root, filename) + + # Replace backslashes with a special character ("⧹") in the filename new_filename = filename.replace("\\", "⧹") + + # Construct the new file path with the modified filename new_file_path = os.path.join(root, new_filename) + + # Rename the file with the modified filename os.rename(file_path, new_file_path) + + # Print the renaming operation details print(f"Renamed {file_path} to {new_file_path}") +# Entry point of the script if __name__ == "__main__": + # Check if the correct number of command-line arguments is provided if len(sys.argv) != 2: print("Usage: python script_name.py ") else: - directory_path = sys.argv[1] + directory_path = sys.argv[1] # Get the directory path from command-line argument + + # Check if the specified directory exists if not os.path.exists(directory_path): print("Directory not found.") else: + # Call the function to replace backslashes in filenames within the directory replace_backslash(directory_path) +