AIM:
Read a text file line by line and display each
word separated by a #
Program Code:
with open('[Link]', 'r') as file:
for line in file:
words = [Link]().split()
print('#'.join(words))
Program Output:
1|Page
AIM:
Write a Python program to read a text file and
display the number of vowels, consonants,
uppercase letters, and lowercase letters present
in the file.
Program Code:
vowels = 'aeiouAEIOU'
vowel_count = consonant_count = uppercase_count =
lowercase_count = 0
with open('E:/file/[Link]', 'r') as file:
text = [Link]()
for char in text:
if [Link]():
if char in vowels:
vowel_count += 1
else:
consonant_count += 1
if [Link]():
uppercase_count += 1
elif [Link]():
2|Page
lowercase_count += 1
print("Vowels:", vowel_count)
print("Consonants:", consonant_count)
print("Uppercase letters:", uppercase_count)
print("Lowercase letters:", lowercase_count)
Program Output:
3|Page
AIM:
Remove all the lines that contain the character 'a' in a file and write it
to another file.
Program Code:
with open('[Link]', 'r') as infile, open('[Link]', 'w') as outfile:
for line in infile:
if 'a' not in [Link]():
[Link](line)
Program Output:
4|Page
AIM:
Write a function in Python to count the number of lines in a text file
[Link] which are starting with the alphabet 'A'.Program
Code:
with open('[Link]', 'r') as file:
for line in file:
words = [Link]().split()
print('#'.join(words))
Program Output:
5|Page
AIM:
Write a Python code to find out the size of the file in bytes, number of lines, and
number of words
Program Code:
f = open('E:/file/[Link]', 'r')
data = [Link]()
print("Size:", len([Link]()), "bytes")
print("Lines:", [Link]('\n') + 1)
print("Words:", len([Link]()))
[Link]()
Program Output:
6|Page
AIM:
Write a program to know the cursor position and print the text according to the
given specifications:
Program Code:
f = open('E:/file/[Link] ', 'r')
print("Initial position:", [Link]())
[Link](3)
print("After moving to 4th position:", [Link]())
[Link]([Link]() + 10)
print("After moving 10 more characters:", [Link]())
print("Current position:", [Link]())
print("Next 10 characters:", [Link](10))
[Link]()
7|Page
Program Output:
8|Page