FILE HANDLING¶
- As the part of programming requirement, we have to store our data permanently for future purpose. For this requirement we should go for files.
- Files are very common permanent storage areas to store our data.
Types of Files¶
There are 2 types of files
- Text Files: Usually we can use text files to store character data Eg: abc.txt
- Binary Files: Usually we can use binary files to store binary data like images,video files, audio files etc...
Opening a File¶
- Before performing any operation (like read or write) on the file,first we have to open that file.For this we should use Python's inbuilt function open()
- But at the time of open, we have to specify mode,which represents the purpose of opening file.
f = open(filename, mode)
f = open("abc.txt","w")
- We are opening abc.txt file for writing data.
Note: All the above modes are applicable for text files. If the above modes suffixed with 'b' then these represents for binary files.
- Eg: rb,wb,ab,r+b,w+b,a+b,xb
Closing a File¶
After completing our operations on the file, it is highly recommended to close the file.
- For this we have to use close() function.
f.close()
The allowed modes in Python are¶
- r: open an existing file for read operation. The file pointer is positioned at the beginning of the file.If the specified file does not exist then we will get FileNotFoundError.This is default mode.
- w: open an existing file for write operation. If the file already contains some data then it will be overridden. If the specified file is not already avaialble then this mode will create that file.
- a: open an existing file for append operation. It won't override existing data.If the specified file is not already avaialble then this mode will create a new file.
- r+: To read and write data into the file. The previous data in the file will not be deleted.The file pointer is placed at the beginning of the file. 5) w+ To write and read data. It will override existing data.
- a+: To append and read data from the file.It wont override existing data.
- x: To open a file in exclusive creation mode for write operation. If the file already exists then we will get FileExistsError.
Various Properties of File Object¶
Once we opend 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("abc.txt",'w')
print("File Name: ",f.name) # abc.txt
print("File Mode: ",f.mode) # w
print("Is File Readable: ",f.readable()) # False
print("Is File Writable: ",f.writable()) # True
print("Is File Closed : ",f.closed) # False
f.close()
print("Is File Closed : ",f.closed) # True
Writing Data to Text Files¶
We can write character data to the text files by using the following 2 methods.
- write(str)
- writelines(list of lines)
f=open("abcd.txt",'w')
f.write("Durga\n")
f.write("Software\n")
f.write("Solutions\n")
print("Data written to the file successfully")
f.close()
Note: In the above program, data present in the file will be overridden everytime if we run the program. Instead of overriding if we want append operation then we should open the file as follows.
f=open("abcd.txt",'w')
list=["sunny\n","bunny\n","vinny\n","chinny"]
f.writelines(list)
print("List of lines written to the file successfully")
f.close()
Note: While writing data by using write() methods, compulsory we have to provide line seperator(\n), otherwise total data should be written to a single line.
Reading Character Data from Text Files¶
We can read character data from text file by using the following read methods.
- read(): To read total data from the file
- read(n): To read 'n' characters from the file
- readline(): To read only one line
- readlines(): To read all lines into a list
f=open("abcd.txt",'r')
data=f.read()
print(data)
f.close()
# o/p
# sunny
# bunny
# vinny
# chinny
f=open("abcd.txt",'r')
data=f.read(10)
print(data)
f.close()
# o/p
# sunny
# bunn
f=open("abcd.txt",'r')
line1=f.readline()
print(line1,end='') # sunny
line2=f.readline()
print(line2,end='') # bunny
line3=f.readline()
print(line3,end='') # vinny
f.close()
f=open("abcd.txt",'r')
lines=f.readlines()
print(lines,end='')
f.close() # ['sunny\n', 'bunny\n', 'vinny\n', 'chinny']
f=open("abcd.txt","r")
print(f.read(3))
print(f.readline())
print(f.read(4))
print("Remaining data")
print(f.read())
o/p
sun
ny
bunn
Remaining data
y
vinny
chinny
The with Statement¶
- The with statement can be used while opening a file. We can use this to group file operation statements within a block.
- The advantage of with statement is it will take care closing of file,after completing all operations automatically even in the case of exceptions also, and we are not required to close explicitly.
with open("abc.txt","w") as f:
f.write("Durga\n")
f.write("Software\n")
f.write("Solutions\n")
print("Is File Closed: ",f.closed) # False
print("Is File Closed: ",f.closed) # True
The seek() and tell() methods¶
tell()¶
-
We can use tell() method to return current position of the cursor(file pointer) from beginning of the file. [ can you plese telll current cursor position]
-
The position(index) of first character in files is zero just like string index.
f=open("abc.txt","r")
print(f.tell())
print(f.read(2))
print(f.tell())
print(f.read(3))
print(f.tell())
Output:
0
su
2
nny
5
seek()¶
We can use seek() method to move cursor(file pointer) to specified location.[Can you please seek the cursor to a particular location]
f.seek(offset, fromwhere)
offset represents the number of positions
The allowed values for second attribute(from where) are
- 0---->From beginning of file(default value)
- 1---->From current position
- 2--->From end of the file Note: Python 2 supports all 3 values but Python 3 supports only zero.
Q Program to print the Number of Lines, Words and Characters present in the given File?¶
import os
import sys
fname=input("Enter File Name: ")
if os.path.isfile(fname):
print("File exists:",fname)
f=open(fname,"r")
else:
print("File does not exist:",fname)
sys.exit(0)
lcount=wcount=ccount=0
for line in f:
lcount=lcount+1
ccount=ccount+len(line)
words=line.split()
wcount=wcount+len(words)
print("The number of Lines:",lcount)
print("The number of Words:",wcount)
print("The number of Characters:",ccount)
o/p
Enter File Name: note.txt
File exists: note.txt
The number of Lines: 12
The number of Words: 98
The number of Characters: 538
Handling Binary Data¶
It is very common requirement to read or write binary data like images,video files,audio files etc.
Q Program to read Image File and write to a New Image File?¶
f1=open("Sunny.jpeg","rb")
f2=open("newpic.jpg","wb")
bytes=f1.read()
f2.write(bytes)
print("New Image is available with the name: newpic.jpg")
Handling CSV Files¶
⚽ CSV : Comma seperated values ⚽ Python provides csv module to handle csv files.
import csv
with open("emp.csv","w",newline='') as f:
w=csv.writer(f) # returns csv writer object
w.writerow(["ENO","ENAME","ESAL","EADDR"])
n=int(input("Enter Number of Employees:"))
for i in range(n):
eno=input("Enter Employee No:")
ename=input("Enter Employee Name:")
esal=input("Enter Employee Salary:")
eaddr=input("Enter Employee Address:")
w.writerow([eno,ename,esal,eaddr])
print("Total Employees data written to csv file successfully")
import csv
f=open("emp.csv",'r')
csv_reader=csv.reader(f) #returns csv reader object
for row in csv_reader:
print(row)
f.close()
o/p
['ENO', 'ENAME', 'ESAL', 'EADDR']
['101', 'ashish', '10000000', 'punjab']
['102', 'sonu', '29999999', 'UP']
['103', 'bindra', '900000000', 'shimla']
['104', 'kumar', '20202020', 'Himachal']
['105', 'sreshti', '1000000000000000', 'GOA']
Note: Observe the difference with newline attribute and without
- with open("emp.csv","w",newline='') as f:
- with open("emp.csv","w") as f:
import csv
with open("emp.csv",'r') as f:
csv_reader=csv.DictReader(f) #returns csv reader object
for row in csv_reader:
print(row)
o/p
{'ENO': '101', 'ENAME': 'ashish', 'ESAL': '10000000', 'EADDR': 'punjab'}
{'ENO': '102', 'ENAME': 'sonu', 'ESAL': '29999999', 'EADDR': 'UP'}
{'ENO': '103', 'ENAME': 'bindra', 'ESAL': '900000000', 'EADDR': 'shimla'}
{'ENO': '104', 'ENAME': 'kumar', 'ESAL': '20202020', 'EADDR': 'Himachal'}
{'ENO': '105', 'ENAME': 'sreshti', 'ESAL': '1000000000000000', 'EADDR': 'GOA'}
Zipping and Unzipping Files¶
It is very common requirement to zip and unzip files. The main advantages are:
- To improve memory utilization
- We can reduce transport time
- We can improve performance.
To perform zip and unzip operations, Python contains one in-bulit module zip file. This module contains a class: ZipFile
To Create Zip File¶
We have to create ZipFile class object with name of the zip file, mode and constant ZIP_DEFLATED. This constant represents we are creating zip file.
f = ZipFile("files.zip","w","ZIP_DEFLATED")
- Once we create ZipFile object,we can add files by using write() method.
f.write(filename)
from zipfile import *
f=ZipFile("files.zip",'w',ZIP_DEFLATED)
f.write("abc.txt")
f.write("abcd.txt")
f.write("note.txt")
f.close()
print("files.zip file created successfully")
To perform unzip Operation¶
We have to create ZipFile object as follows
-
f = ZipFile("files.zip","r",ZIP_STORED)
-
ZIP_STORED represents unzip operation. This is default value and hence we are not required to specify.
- Once we created ZipFile object for unzip operation, we can get all file names present in that zip file by using namelist() method.
names = f.namelist()
from zipfile import *
f=ZipFile("files.zip",'r',ZIP_STORED)
names=f.namelist()
for name in names:
print( "File Name: ",name)
o/p
File Name: abc.txt
File Name: abcd.txt
File Name: note.txt
Assignments¶
46. Python Program to Find Hash of File¶
# importing the hashlib module
import hashlib
def hash_file(filepath):
"""
This function returns the SHA-1 hash
of the file passed into it"""
# make a hash object
h = hashlib.sha1()
# open file for reading in binary mode
with open(filepath,'rb') as file:
# loop till the end of the file
chunk = 0
while chunk != b'':
# read only 1024 bytes at a time
chunk = file.read(1024)
h.update(chunk)
# return the hex representation of digest
return h.hexdigest()
message = hash_file("data_file.txt") # Use path of the file
print(message)
49. Python Program to Safely Create a Nested Directory¶
45. Python Program to Find the Size (Resolution) of an Image¶
def jpeg_res(filename):
""""This function prints the resolution of the jpeg image file passed into it"""
# open image for reading in binary mode
with open(filename,'rb') as img_file:
# height of image (in 2 bytes) is at 164th position
img_file.seek(163)
# read the 2 bytes
a = img_file.read(2)
# calculate height
height = (a[0] << 8) + a[1]
# next 2 bytes is width
a = img_file.read(2)
# calculate width
width = (a[0] << 8) + a[1]
print("The resolution of the image is",width,"x",height)
jpeg_res("colored_terminal.png")
57. Python Program to Copy a File¶
66. Python Program Read a File Line by Line Into a List¶
# Example 1: Using readlines()
with open("data_file.txt") as f:
content_list = f.readlines()
print(content_list)
content_list = [x.strip() for x in content_list]
print(content_list)
# Example 2: Using for loop and list comprehension
with open('data_file.txt') as f:
content_list = [line.rstrip() for line in f]
print(content_list)
69. Python Program to Append to a File¶
72. Python Program to Extract Extension From the File Name¶
# Example 1: Using splitext() method from os module
import os
file_details = os.path.splitext('/path/file.ext')
print(file_details)
print(file_details[1])
78. Python Program to Get the File Name From the File Path¶
# Example 1: Using os module
import os
# file name with extension
file_name = os.path.basename('/root/file.ext')
# file name without extension
print(os.path.splitext(file_name)[0])
81. Python Program to Get Line Count of a File¶
num_of_lines = sum(1 for l in open('data_file.txt'))
print(num_of_lines)
Homework:¶
- Add the top 5 most common words to the analysis message.
- Find the total number of spaces in the file.
- Find the total number of characters in the file (including spaces).
- Find the total number of characters in the file (excluding spaces).
- Find the total number of words in the file.
- Find the total number of lines in the file.
- Display the file name and list of directories containing the file.
- Calculate the average word length in the file.
- Find the number of unique words in the file.
- Count the frequency of a specific word provided by the user.
- Identify and display all unique punctuation marks in the file.
- Check if the file contains any numerical digits and count them.
- Find the longest word in the file.
- Find the shortest word in the file.
- Sort all the words alphabetically and display them.