-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRead-and-Write-Files.py
24 lines (18 loc) · 1.06 KB
/
Read-and-Write-Files.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#This code reads the user's text file, alter it and writes a new file with the corrections done.
try:
with open('file.txt', 'r') as file: # Open the file for reading.
text = file.read() # Read the contents of the file under the variable 'text'.
char_up = text.upper() # Convert the text to uppercase using the .upper() function.
char_len = len(text) # Count the total number of characters in text using the len() function.
vowels = ['a', 'e', 'i', 'o', 'u'] # Place 'a', 'e', 'i', 'o', and 'u' under vowels.
new_text = ''.join(['*' if char.lower() in vowels else char for char in char_up]) # Replace all vowels with asterisk.
with open('new_text.txt', 'w') as file: # Write the modified text to a new file.
file.write(new_text)
file.write(f"\nTotal number of characters: {char_len}")
# Make sure to handle any possible errors that may occur during file reading, writing or string operations with appropriate exception handling.
except FileNotFoundError:
print("Error. File not found")
else:
pass
finally:
pass