-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path10. Q8.py
More file actions
28 lines (22 loc) · 1.03 KB
/
10. Q8.py
File metadata and controls
28 lines (22 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
'''Given a text file, write a program to create another text file deleting
the words ‘a’, ‘the’, ‘an’ and replacing each one of them with a blank space.'''
# Define file paths
input_file = "input.txt" # The original text file
output_file = "output.txt" # The new file after removing words
# List of words to be replaced
words_to_remove = ['a', 'the', 'an']
try:
# Read the content of the input file
with open(input_file, "r") as infile:
content = infile.read()
# Replace the specified words with a blank space
for word in words_to_remove:
content = content.replace(f" {word} ", " ") # Replace surrounded by spaces
# Write the modified content to the output file
with open(output_file, "w") as outfile:
outfile.write(content)
print(f"Words 'a', 'the', 'an' have been removed and saved to '{output_file}'.")
except FileNotFoundError:
print(f"Error: The file '{input_file}' was not found.")
except Exception as e:
print("An error occurred:", e)