1. Read a text file line by line and display each word separated by a #.
# Open file in read mode
with open("[Link]", "r") as file:
for line in file:
# Remove leading/trailing whitespace and split into words
words = [Link]( ).split( )
# Join words with #
output = "#".join(words)
# Print result
print(output)
2. Read a text file and display the number of vowels /consonants/uppercase/lowercase
characters in the file.
# Open file in read mode
with open("[Link]", "r") as file:
text = [Link]( )
# Initialize counters
vowels = consonants = uppercase = lowercase = 0
# Define vowel set
vowel_set = "aeiouAEIOU"
# Iterate through each character
for ch in text:
if [Link]( ): # Only letters
if ch in vowel_set:
vowels += 1
else:
consonants += 1
if [Link]( ):
uppercase += 1
elif [Link]():
lowercase += 1
# Display results
print("Number of vowels:", vowels)
print("Number of consonants:", consonants)
print("Number of uppercase letters:", uppercase)
print("Number of lowercase letters:", lowercase)
3. Create a binary file with name and roll number .Search for a given roll number and
display the name,if not found display appropriate message.
import pickle
# Function to create a binary file
def create_file( ):
with open("[Link]", "wb") as f:
n = int(input("Enter number of students: "))
for _ in range(n):
roll = int(input("Enter Roll Number: "))
name = input("Enter Name: ")
# Save record as tuple
record = (roll, name)
[Link](record, f)
print("File '[Link]' created successfully.\n")
# Function to search for a roll number
def search_roll(roll_no):
found = False
with open("[Link]", "rb") as f:
try:
while True:
record = [Link](f) # (roll, name)
if record[0] == roll_no:
print(f"Roll Number {roll_no} found. Name: {record[1]}")
found = True
break
except EOFError:
pass
if not found:
print(f"Roll Number {roll_no} not found in '[Link]'.")
# Main Program
create_file( ) # Step 1: Create File
roll_no = int(input("Enter roll number to search: ")) # Step 2: Search
search_roll(roll_no)
4. Create a binary file with roll number,name and marks. Input a roll number and update
the marks.
import pickle
import os
# Step 1: Create a binary file with roll, name, marks
def create_file( ):
with open("[Link]", "wb") as f:
n = int(input("Enter number of students: "))
for _ in range(n):
roll = int(input("Enter Roll Number: "))
name = input("Enter Name: ")
marks = float(input("Enter Marks: "))
record = (roll, name, marks)
[Link](record, f)
print("File '[Link]' created successfully.\n")
# Step 2: Update marks for a given roll number
def update_marks(roll_no, new_marks):
records = [ ]
found = False
# Read all records
with open("[Link]", "rb") as f:
try:
while True:
record = [Link](f)
[Link](record)
except EOFError:
pass
# Update the marks
for i in range(len(records)):
if records[i][0] == roll_no:
name = records[i][1]
records[i] = (roll_no, name, new_marks)
found = True
break
# Write back to file
with open("[Link]", "wb") as f:
for record in records:
[Link](record, f)
if found:
print(f"Marks updated for Roll Number {roll_no}.")
else:
print(f"Roll Number {roll_no} not found in '[Link]'.")
# Main Program
create_file( )
roll_no = int(input("Enter roll number to update marks: "))
new_marks = float(input("Enter new marks: "))
update_marks(roll_no, new_marks)
import pickle
def display_file( ):
print("\nContents of '[Link]':")
with open("[Link]", "rb") as f:
try:
while True:
record = [Link](f)
print(f"Roll No: {record[0]}, Name: {record[1]}, Marks: {record[2]}")
except EOFError:
pass
# Call function
display_file( )
5. Remove all the lines that contain the character ‘a’ in a file and write it to another file.
# Remove all lines containing 'a' and display both input and output files
def remove_lines():
# Step 1: Display [Link]
print("Contents of [Link]:")
with open("[Link]", "r") as fin:
input_lines = [Link]()
for line in input_lines:
print([Link]( ))
# Step 2: Process and write to [Link]
with open("[Link]", "w") as fout:
for line in input_lines:
if 'a' not in line: # keep only lines without 'a'
[Link](line)
# Step 3: Display [Link]
print("\nContents of [Link]:")
with open("[Link]", "r") as fout:
for line in fout:
print([Link]( ))
# Main Program
remove_lines( )