File Handling Book Programs
File Handling Book Programs
txt
welcomw2ckspe
cbse school 2023
Academic year 2023 is the best year
PROGRAM 16
f=open("an.txt","r")
for i in f:
for j in i:
if j.isdigit():
print(j)
OUTPUT
2
2
0
2
3
2
0
2
3
ss.txt
My first book
was me and
My family. It
gave me
chance to be
known to the
world
PROGRAM 19
def countmemy():
f=open("ss.txt","r")
n=f.read()
print(n)
m=n.split()
print(m)
n=0
for x in m:
if x=="me" or x=="My":
print(x)
n+=1
f.close()
print("The count of Me and My is:",n)
countmemy()
OUTPUT
My first book
was me and
My family.It
gave me
chance to be
known to the
world
['My', 'first', 'book', 'was', 'me', 'and', 'My', 'family.It', 'gave', 'me', 'chance', 'to',
'be', 'known', 'to', 'the', 'world']
My
me
My
me
The count of Me and My is: 4
data.txt
Line 1\n
\n
Line 3
Line 4
\n
Line 6
PROGRAM 20
f=open("data.txt","r")
lst=f.readlines()
print(lst[0], end='')
print(lst[2], end='')
print(lst[5], end='')
print(lst[1], end='')
print(lst[4], end='')
print(lst[3])
OUTPUT
Line 1\n
Line 3
Line 6\n
\n
Line 4
text.txt
Life is like riding a bicycle. To keep your balance, you must keep moving.
When I'm not feeling my best I ask myself, "What are you gonna do about it?
Sometimes you win, sometimes you learn.
PROGRAM 21
f=open("text.txt","r")
a=f.readlines()
print("The last line in the text file is :", a[-1])
OUTPUT
The last line in the text file is : Sometimes you win, sometimes you learn.
source.txt
@ Your limitation it's only your imagination.
Push yourself, because no one else is going to do it for you.
Sometimes later becomes never. ...
@ Great things never come from comfort zones.
@To live a creative life, we must lose our fear of being wrong.
@Dream it. ...
PROGRAM 22
def filter(s,t):
f1=open(s,"r")
f2=open(t,"w")
while True:
line=f1.readline()
if len(line)==0:
break
if line[0]=='@':
f2.write(line)
f1.close()
f2.close()
filter("source.txt","target.txt")
OUTPUT
@ Your limitation it's only your imagination.
@ Great things never come from comfort zones.
@To live a creative life, we must lose our fear of being wrong.
@Dream it. ...