My issue
What’s wrong with this code ![]()
My code
def user_function():
# Define the input file path
file_path = "/mnt/codechef/input.txt"
try:
# Read the content of the input file
with open(file_path, 'r') as file:
content = file.read()
# Define the search and replace strings
search = "Codechef"
replace = "CodeChef"
# Replace occurrences of search string with replace string
modified_content = content.replace(search, replace)
# Write the modified content back to the file
with open(file_path, 'w') as file:
file.write(modified_content)
# Display the modified file contents
print("Modified file contents:")
print(modified_content)
except IOError as e:
print("An error occurred:", e)
Learning course: Python Programming
Problem Link: Replace word in Python Programming