Professional Documents
Culture Documents
DATA FILE HANDLING Part 1 Programs
DATA FILE HANDLING Part 1 Programs
Part A
Text File( PROGRAMS)
1. Write a function to enter a string in a file “Story.txt”, then read the same file
and count how many capital vowels are available in the file.
def DIS():
x=open("Story.txt","w")
S=input("Enter String")
x.write(S)
x.close()
y=open("story.txt","r")
a=y.read()
count=0
for i in a:
if i in "AEIOU":
count=count+1
print("Total Capital Vowels",count)
y.close()
DIS()
OUTPUT:
Enter String NARENDRA srivastava IS class TEACHER of XIIA
Total Capital Vowels 10
2.Write a program to enter a sentence and print all words whose length is
more than 5.
def DIS():
x=open("Story.txt","w")
S=input("Enter String:")
x.write(S)
x.close()
y=open("story.txt","r")
a=y.read()
c=a.split()
count=0
for i in c:
if len(i) >5:
print(i)
y.close()
DIS()
OUTPUT:
3. Write a program to enter a sentence and print all words which are starting
with “R”.
def DIS():
x=open("Story.txt","w")
S=input("Enter String")
x.write(S)
x.close()
y=open("story.txt","r")
a=y.read()
c=a.split()
for i in c:
if i[0]=="R":
print(i)
y.close()
DIS()
OUTPUT:
Enter StringRavis Shatri Run away form Rome
Ravis
Run
Rome
ST=i
print(ST)
y.close()
DIS()
OUTPUT:
Narendra srivastava is working with Doon International school
Longest Word= International
5. Write a program to read a file “Para.txt” and replace each space with
“#”..
N=open("Para.txt","w")
N.write("Hello Students I am replacing each space with # symbol")
N.close()
S=open("Para.txt","r+")
x=S.read()
print(x)
St=" "
for i in x:
if i==' ':
St=St+"#"
else:
St=St+i
S.seek(0)
S.write(St)
x=S.read()
print(St)
S.close()
OUTPUT:
Hello Students I am replacing each space with # symbol
Hello#Students#I#am#replacing#each#space#with###symbol
Q6.Write a program to read a text file “scinec.txt” and count how many times
word “this” is written?
x=open("Science.txt","r")
y=x.read()
z=y.split()
count=0
for i in z:
if i=="this":
count=count+1
print(count)
x.close()
Q7. Write a function to read a text file “Story.txt” and print all lines which are
starting with “A”.
def PRINT():
a=open(“Story.txt”,”r”)
y=a.readline()
while y:
if y[0]==”A”:
print(y)
y=a.readline()
a.close()
Q8. Write a program to read a file “Article.Txt” and print each alternate line.
def ALTERNATE():
a=open(“Article.txt”,”r”)
y=a.readlines()
c=1
for i in y:
if i%2==1:
print(i)
i=i+1
a.close()
Q9. Write a function to read a file “English.Txt” and print longest line.
def Longest():
a=open(“English.txt”,”r”)
y=a.readlines()
c=0
x=” “
for i in y:
if len(i)>c:
c=len(i)
x=i
print(x)
a.close()
Q10. Write a program to read a file “Stf.txt” and print all line form top to bottom
as well as bottom to top.
x=open("Science.txt","w")