Add: Code comments

This commit is contained in:
Aroy-Art 2023-12-20 21:36:34 +01:00
parent 37aeb66d99
commit 188dd1c57b
Signed by: Aroy
GPG key ID: DB9689E9391DD156

View file

@ -1,22 +1,40 @@
import os import os
import sys import sys
# Function to replace backslashes in filenames within a directory
def replace_backslash(directory): def replace_backslash(directory):
# Traverse through the directory and its subdirectories
for root, _, files in os.walk(directory): for root, _, files in os.walk(directory):
for filename in files: for filename in files:
# Check if the filename contains a backslash
if "\\" in filename: if "\\" in filename:
# Construct the full file path
file_path = os.path.join(root, filename) file_path = os.path.join(root, filename)
# Replace backslashes with a special character ("") in the filename
new_filename = filename.replace("\\", "") new_filename = filename.replace("\\", "")
# Construct the new file path with the modified filename
new_file_path = os.path.join(root, new_filename) new_file_path = os.path.join(root, new_filename)
# Rename the file with the modified filename
os.rename(file_path, new_file_path) os.rename(file_path, new_file_path)
# Print the renaming operation details
print(f"Renamed {file_path} to {new_file_path}") print(f"Renamed {file_path} to {new_file_path}")
# Entry point of the script
if __name__ == "__main__": if __name__ == "__main__":
# Check if the correct number of command-line arguments is provided
if len(sys.argv) != 2: if len(sys.argv) != 2:
print("Usage: python script_name.py <directory_path>") print("Usage: python script_name.py <directory_path>")
else: 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): if not os.path.exists(directory_path):
print("Directory not found.") print("Directory not found.")
else: else:
# Call the function to replace backslashes in filenames within the directory
replace_backslash(directory_path) replace_backslash(directory_path)