import argparse def replace_word_in_file(file_path, search_word, replace_word): try: with open(file_path, 'r') as file: file_data = file.read() updated_data = file_data.replace(search_word, replace_word) with open(file_path, 'w') as file: file.write(updated_data) print(f"Replaced '{search_word}' with '{replace_word}' in {file_path}") except FileNotFoundError: print(f"File '{file_path}' not found.") except Exception as e: print(f"An error occurred: {str(e)}") if __name__ == "__main__": parser = argparse.ArgumentParser(description="Search and replace a word in a file.") parser.add_argument("--file_path", required=True, help="Path to the file in which to perform the replacement.") parser.add_argument("--search_word", required=True, help="Word to search for in the file.") parser.add_argument("--replace", required=True, help="Word to replace the search word with.") args = parser.parse_args() replace_word_in_file(args.file_path, args.search_word, args.replace)