226k views
0 votes
Write a program that prompt the user for a file name and a word then removes all the occurrences of the specified word from that file. The program should create a new file called "processedData.txt" that contains the text after removing these occurrences.

1 Answer

3 votes

Final answer:

To remove all occurrences of a specified word from a file, use the Python code provided that prompts the user for the file name and the word and creates a new file with the modified content.

Step-by-step explanation:

To write a program that removes all occurrences of a specified word from a file, you can use the following Python code:

def remove_word(file_name, word):
with open(file_name, 'r') as file:
content = file.read()
content = content.replace(word, '')
with open('processedData.txt', 'w') as file:
file.write(content)

file_name = input('Enter the file name: ')
word = input('Enter the word to remove: ')
remove_word(file_name, word)

This code defines a function remove_word that takes the file name and the word as input. It reads the content of the file, removes all occurrences of the specified word using the replace method, and then writes the modified content to a new file called processedData.txt.

User Loviji
by
8.1k points