import re import os def replace_css_links(directory): affected_files = [] changes = {} # Collect affected files and changes for filename in os.listdir(directory): if re.match(r"\d+_\d+_index\.html", filename): file_path = os.path.join(directory, filename) with open(file_path, "r", encoding="utf-8") as file: content = file.read() match = re.match(r"(\d+)_(\d+)_index\.html", filename) if match: new_stylesheet = f"{match.group(1)}_{match.group(2)}_style.css" if '' in content: affected_files.append(filename) changes[filename] = new_stylesheet # Display affected files if affected_files: print("Files to be affected:") for file in affected_files: print(f"- {file}: style.css -> {changes[file]}") else: print("No files will be affected.") return # User confirmation user_input = input( "Do you want to proceed with these changes? (Y/n): ").strip().lower() if user_input != 'y' and user_input != '': print("Operation cancelled.") return # Execute changes for filename in affected_files: file_path = os.path.join(directory, filename) with open(file_path, "r", encoding="utf-8") as file: content = file.read() modified_content = re.sub( r'', f'', content ) with open(file_path, "w", encoding="utf-8") as file: file.write(modified_content) print(f"Updated {filename}.") print("All changes have been completed.") # Usage example # replace_css_links("./")