#!/usr/bin/env python3 def find_and_replace(file_path, old_word, new_word, case_sensitive=True, create_backup=True): """ Find and replace all occurrences of a word in a file. Args: file_path (str): Path to the file old_word (str): Word to find new_word (str): Word to replace it with case_sensitive (bool): Whether to perform case-sensitive replacement create_backup (bool): Whether to create a backup of the original file Returns: int: Number of replacements made """ try: # Read the file content with open(file_path, 'r') as file: content = file.read() # Create backup if requested if create_backup: backup_path = file_path + '.bak' with open(backup_path, 'w') as backup_file: backup_file.write(content) print(f"Backup created at {backup_path}") # Perform the replacement if case_sensitive: new_content = content.replace(old_word, new_word) count = content.count(old_word) else: import re count = len(re.findall(re.escape(old_word), content, re.IGNORECASE)) new_content = re.sub(re.escape(old_word), new_word, content, flags=re.IGNORECASE) # Write the modified content back to the file if count > 0: with open(file_path, 'w') as file: file.write(new_content) print(f"Successfully replaced {count} occurrence(s) of '{old_word}' with '{new_word}'.") else: print(f"No occurrences of '{old_word}' found in the file.") return count except FileNotFoundError: print(f"Error: File '{file_path}' not found.") return 0 except Exception as e: print(f"Error: {str(e)}") return 0 def find_and_replace_in_line_range(file_path, old_word, new_word, start_line, end_line, case_sensitive=True): """ Find and replace occurrences of a word within a specific range of lines. Args: file_path (str): Path to the file old_word (str): Word to find new_word (str): Word to replace it with start_line (int): Start line number (1-based index) end_line (int): End line number (1-based index) case_sensitive (bool): Whether to perform case-sensitive replacement Returns: int: Number of replacements made """ try: # Read all lines from the file with open(file_path, 'r') as file: lines = file.readlines() total_lines = len(lines) # Validate line range if start_line < 1: start_line = 1 print("Warning: Start line adjusted to 1") if end_line > total_lines: end_line = total_lines print(f"Warning: End line adjusted to {total_lines}") if start_line > end_line: print("Error: Start line cannot be greater than end line") return 0 # Track replacements total_replacements = 0 # Process each line in the range for i in range(start_line - 1, end_line): if case_sensitive: count = lines[i].count(old_word) if count > 0: lines[i] = lines[i].replace(old_word, new_word) total_replacements += count else: import re pattern = re.escape(old_word) count = len(re.findall(pattern, lines[i], re.IGNORECASE)) if count > 0: lines[i] = re.sub(pattern, new_word, lines[i], flags=re.IGNORECASE) total_replacements += count # Write the modified content back to the file if changes were made if total_replacements > 0: with open(file_path, 'w') as file: file.writelines(lines) print(f"Successfully replaced {total_replacements} occurrence(s) of '{old_word}' with '{new_word}' between lines {start_line}-{end_line}.") else: print(f"No occurrences of '{old_word}' found between lines {start_line}-{end_line}.") return total_replacements except FileNotFoundError: print(f"Error: File '{file_path}' not found.") return 0 except Exception as e: print(f"Error: {str(e)}") return 0 # Example usage as command line tool if __name__ == "__main__": import argparse parser = argparse.ArgumentParser(description='Find and replace text in files') parser.add_argument('file_path', type=str, help='Path to the file') parser.add_argument('old_word', type=str, help='Word to find') parser.add_argument('new_word', type=str, help='Word to replace it with') parser.add_argument('--no-case-sensitive', dest='case_sensitive', action='store_false', help='Perform case-insensitive replacement') parser.add_argument('--no-backup', dest='create_backup', action='store_false', help='Do not create backup of the original file') parser.add_argument('--start-line', type=int, default=None, help='Start line for replacement (use with --end-line)') parser.add_argument('--end-line', type=int, default=None, help='End line for replacement (use with --start-line)') args = parser.parse_args() # Check if line range is specified if (args.start_line is not None) and (args.end_line is not None): find_and_replace_in_line_range( args.file_path, args.old_word, args.new_word, args.start_line, args.end_line, args.case_sensitive ) else: find_and_replace( args.file_path, args.old_word, args.new_word, args.case_sensitive, args.create_backup )
mapinstead of a loop: stackoverflow.com/questions/26986229/…, that's what I went with