Add: Code comments
This commit is contained in:
parent
37aeb66d99
commit
188dd1c57b
1 changed files with 19 additions and 1 deletions
|
@ -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 <directory_path>")
|
||||
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)
|
||||
|
||||
|
|
Loading…
Reference in a new issue