Fr1.
py
"""
Created on Mon Nov 9 11:38:59 2020
@author: Hp
"""
try:
f=open("mydata.txt","r")
s=f.read()
print("The content of a file:",s)
except Exception:
print("Error")
fr2.py
"""
Python program to read a line of text from keyboard and
write it to a file inputted by the user.
"""
x=input("Enter a file name:")
try:
k=open(x,"w")
s=input("Enter a line of text")
k.write(s)
except Exception:
print("Error")
finally:
k.close()
fr3.py
"""
Python program to read each line from a file biodata
and count number of lines using readline() function
"""
count=0
try:
h=open("biodata.txt","r")
while True:
k=h.readline()
if k:
print(k)
count=count+1
else:
break
except Exception:
print("Error")
else:
print("Total number of lines:",count)
fr4.py
"""
Python program to read each line from a file biodata
and count number of lines using readlines() function
"""
count=0
try:
h=open("biodata.txt","r")
l=h.readlines()
print(l)
for k in l:
print(k)
count=count+1
except Exception:
print("Error")
else:
print("Total number of lines:",count)
fr5.py
"""
Python program to read each line from a file biodata
and count number of lines using readlines() function
"""
count=0
try:
h=open("biodata.txt","r")
for i in h:
print(i)
count=count+1
except Exception:
print("Error")
else:
print("Total number of lines:",count)
fr6.py
"""
Python program to accept string from user until user press @ symbol.
Write the inputted string to a file input by the user
"""
s=input("Enter a file name:")
print("Enter a string and exit by pressing @ symbol")
try:
f=open(s,"w")
m=''
while m!='@':
m=input("Enter a string:")
if m!='@':
f.write(m+'\n')
except Exception:
print("Error")
finally:
f.close()
fr7.py
"""
Python program to count number of lines words
and characters from a given text file
"""
lc=0
wc=0
cc=0
try:
p=open("biodata.txt","r")
for j in p:
print(j)
lc=lc+1
wr=j.split(' ')
wc=wc+len(wr)
cc=cc+len(j)
print("Number of lines:",lc)
print("Number of words:",wc)
print("Number of characters:",cc)
except Exception:
print("Error")
finally:
p.close()
fr8.py
"""
Python program to read a lines from text file
that starts with from and print it on screen
"""
try:
m=open("s.txt","r")
while True:
k=m.readline()
if k:
if k.startswith("from"):
print(k.strip())
else:
break
except Exception:
print("Error")
finally:
m.close()
fr9.py
"""
Python program that generates the factorial of all integers from 1 to 10
and stores them in a file called fact.txt
"""
import math
try:
f=open("fact.txt","w")
for i in range(1,11):
f.write("The factorial of {} is {}\n".format(i,math.factorial(i)))
except Exception:
print("Error")
finally:
f.close()
finally:
f.close()
f1.close()
f2.close()
10
"""
Python program to read the numbers stored in a file test.txt.
Check the number is even or odd. If even
write it to a file even.txt otherwise write it to a file odd.txt
"""
f=open("num.txt")
f1=open("even.txt","w")
f2=open("odd.txt","w")
while True:
k=f.readline()
if k:
if int(k)%2==0:
f1.write(k+'\n')
else:
f2.write(k+'\n')
else:
break
f.close()
f1.close()
f2.close()
11.
"""
Python program to copy an image file into another file
"""
f=open("D:\p1.jpg","rb")
f1=open("p10.jpg","wb")
b=f.read()
f1.write(b)
f.close()
f1.close()