You are on page 1of 3

1. Assuming that a text file named first.

txt contains some


text written into it, write a function that reads the file
first.txt and creates a new file named second.txt, to
contain only those words from the file first.txt which
start with a lowercase vowel (i.e. with 'a', 'e', 'i', 'o',
'u').
f1= open("first.txt")

new=""

while True:

x = f1.readline()

if len(x)<=0:

break

y = x.split()

for i in y:

if i[0] in "AEIOUaeiou":

new+=i+ " "

new+="\n"

f2= open("second.txt", "w+")

f2.write(new)

f1.close()

f2.close()
2. Write a menu driven program to add and display data
from a text file. The text file contains Student details (Roll
no, name and marks).

while True:

file = input("Enter file name: ")

f1=open(file, "r+")

print("""Choose an option:

1. Add data

2. Read contents""")

x = input("Enter option: ")

if x == "1":

student = []

ch = "y"

while ch=="y" or ch =="Y":

print()

rno = int(input("Enter roll no. : "))

student.append(str(rno)+",")

name = input("Enter name: ")

name = name.upper()

student.append(name+",")

marks = input("Enter total marks: ")

student.append(marks + "\n")

ch = input("Do you want to add more entries? (y/n): ")

if ch == 'y' or ch =="Y":

continue

else:

f1.writelines(student)
if x =="2":

while True:

y = f1.readline()

if len(y)<=0:

break

print(y, end = "")

z = input("Do you wish to continue with the code? (Y/N): ")

if z =="n" or z=="N":

break

You might also like