You are on page 1of 5

Narendra Srivastava, Doon International School

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()

Data File Handling Text File Part A, Theory


Narendra Srivastava, Doon International School

DIS()

OUTPUT:

Enter String: This is an example of split() function


example
split()
function

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

4.Write a function to read a file “Story.txt” and print longest word.


def DIS():
y=open("story.txt","r")
a=y.read()
print(a)
c=a.split()
ST=" "
l=0
for i in c:
if len(i)>l:
l=len(i)

Data File Handling Text File Part A, Theory


Narendra Srivastava, Doon International School

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)

Data File Handling Text File Part A, Theory


Narendra Srivastava, Doon International School

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")

Data File Handling Text File Part A, Theory


Narendra Srivastava, Doon International School

x.write("Hello Studets\nToday we will learn\nprinting data file\nin reverse


order")
x.close()
y=open("Science.txt","r")
z=y.readlines()
print("Data of file line wise")
for r in z:
print(r)
L=len(z)
print("Data of file line wise in reverse order")
for i in range(L-1,-1,-1):
print(z[i])
y.close()

Data File Handling Text File Part A, Theory

You might also like