Aim: Python Programs on File Handling
Description : When we open a file and we got file object, we can get various details related to that file
by using its properties.
name: Name of opened file
mode: Mode in which the file is opened
closed: Returns boolean value indicates that file is closed or not
readable(): Retruns boolean value indicates that whether file is readable or not
writable(): Returns boolean value indicates that whether file is writable or not.
f=open("Departments.txt",'r')
print("file name :",f.name)
print("file mode :",f.mode)
print("is file readable :",f.readable())
print("is file writable :", f.writable())
print("is file closed :", f.closed)
f.close()
print("is file closed :",f.closed)
Output:
file name : Departments.txt
file mode : r
is file readable : True
is file writable : False
is file closed : False
is file closed : True
a)Write a Python program to perform read and write operations on a file.
str =input("Enter the data into a file : ")
f1=open('Departments.txt','w')
f1.write(str)
f1.close()
f2=open('Departments.txt','r')
data=f2.read()
print(data)
f2.close()
Output:
Enter the data into a file : Information Technology
Information Technology
b) Write a Python program to copy the contents of one file to another file.
filename1 = input("Enter the file name 1: ")
f1 = open(filename1,'w')
f1.write('Computer science Engineering\n')
f1.write('Elecronics and Communication Engineering\n')
f1.write('Civil Engineering\n')
f1.close()
f2 = open(filename,'r')
data1 = f2.read()
print(data)
f2.close()
filename2 = input("Enter the file name 2: ")
f3 = open(filename2,'w')
f3.write(data1)
f3.close()
f3 = open(filename2,'r')
data2 = f3.read()
print(data2)
f3.close()
Output:
Enter the file name 1: Departments
Computer science Engineering
Elecronics and Communication Engineering
Civil Engineering
Enter the file name 2: college
Computer science Engineering
Elecronics and Communication Engineering
Civil Engineering
c) Write a Python program to count frequency of characters in a given file.
filename = input("Enter a file name : ")
f1 = open(filename,'w')
f1.write('Computer science Engineering\n')
f1.write('Elecronics and Communication Engineering\n')
f1.write('Civil Engineering\n')
f1.close()
f1 = open(filename,'r')
data = f1.read()
a =list(set(data))
sorted(a)
print(a)
f1.close()
for i in a:
print("{} as occured {} times".format(i,data.count(i)))
output:
Enter a file name : Departments
['o', 'u', 'e', 'E', 'n', 'v', 'l', 'g', ' ', 'd', 's', 'c', 'r', 'C',
'p', 'a', 't', 'm', 'i','\n']
o as occured 4 times
u as occured 2 times
e as occured 10 times
E as occured 4 times
n as occured 14 times
v as occured 1 times
l as occured 2 times
g as occured 6 times
as occured 6 times
d as occured 1 times
s as occured 2 times
c as occured 5 times
r as occured 5 times
C as occured 3 times
p as occured 1 times
a as occured 2 times
t as occured 2 times
m as occured 3 times
i as occured 12 times
as occured 3 times
d) Write a Python program to print each line of a file in reverse order
filename = input("Enter a file name : ")
f1 = open(filename,'w')
f1.write('Computer science Engineering\n')
f1.write('Elecronics and Communication Engineering\n')
f1.write('Civil Engineering\n')
f1.write('madam arora level')
f1.close()
f1 = open(filename,'r')
data = f1.readlines()
print(data)
for i in data:
print(i[::-1])
f1.close()
Output:
Enter a file name : Departments
['Computer science Engineering\n', 'Elecronics and Communication
Engineering\n', 'Civil Engineering\n', 'madam arora level']
gnireenignE ecneics retupmoC
gnireenignE noitacinummoC dna scinorcelE
gnireenignE liviC
level arora madam
e) Write a Python program to compute the number of characters, words and lines in a file.
filename = input("Enter a file name : ")
c = 0
w = 0
l = 0
f1 = open(filename,'w')
f1.write('Computer science Engineering\n')
f1.write('Elecronics and Communication Engineering\n')
f1.write('Civil Engineering\n')
f1.close()
f2 = open(filename,'r')
data = f2.readlines()
print(data)
for i in data:
c = c + len(i)
w = w + len(i.split())
l = l + 1
f2.close()
print('The number of Characters are :', c)
print('The number of Words are :', w)
print('The number of Lines are :', l)
output:
Enter a file name : Departments
['Computer science Engineering\n', 'Elecronics and Communication
Engineering\n', 'Civil Engineering\n']
The number of Characters are : 88
The number of Words are : 9
The number of Lines are : 3