SlideShare a Scribd company logo
Course Id :INT 213
FILE HANDLING
I INTRODUCTION
FILES
• Data is very important. Every organization depends on its data for
continuing its business operations. If the data is lost, the organization
has to be closed.
• This is the reason computers are primarily created for handling data,
especially for storing and retrieving data. In later days, programs are
developed to process the data that is stored in the computer.
FILES
• To store data in a computer, we need files. For example, we can
store employee data like employee number, name and salary in a file
in the computer and later use it whenever we want.
• Similarly, we can store student data like student roll number, name
and marks in the computer. In computers’ view, a file is nothing but
collection of data that is available to a program. Once we store data
in a computer file, we can retrieve it and use it depending on our
requirements.
•
File Handling Btech computer science and engineering ppt
ADVANTAGES OF STORING A DATA IN A FILE
• When the data is stored in a file, it is stored permanently. This means
that even though the computer is switched off, the data is not
removed from the memory since the file is stored on hard disk or CD.
This file data can be utilized later, whenever required.
• It is possible to update the file data. For example, we can add new
data to the existing file, delete unnecessary data from the file and
modify the available data of the file. This makes the file more useful.
ADVANTAGES OF STORING A DATA IN A FILE
• Once the data is stored in a file, the same data can be shared by
various programs. For example, once employee data is stored in a
file, it can be used in a program to calculate employees’ net salaries
or in another program to calculate income tax payable by the
employees.
• Files are highly useful to store huge amount of data. For example,
voters’ list or census data.
II TYPES OF FILES
TYPES OF FILES
• In Python, there are two types of files.
• They are:
 Text files
Binary files
• Text files store the data in the form of characters. For example, if we
store employee name “Ganesh”, it will be stored as 6 characters and the
employee salary 8900.75 is stored as 7 characters.
• Text files are used to store characters or strings.
TYPES OF FILES
• Binary files store entire data in the form of bytes, i.e. a group of 8 bits
each. For example, a character is stored as a byte and an integer is
stored in the form of 8 bytes (on a 64 bit machine). When the data is
retrieved from the binary file, the programmer can retrieve the data
as bytes.
• Binary files can be used to store text, images, audio and video.
Image files are generally available in .jpg, .gif or .png formats.
• We cannot use text files to store images as the images do not
contain characters.
TYPES OF FILES
• On the other hand, images contain pixels which are minute dots with
which the picture is composed of.
• Each pixel can be represented by a bit, i.e. either 1 or 0. Since these
bits can be handled by binary files, we can say that they are highly
suitable to store images. It is very important to know how to create
files, store data in the files and retrieve the data from the files in
Python
III READ A FILE
OPENING A FILE
• file = open("file1.txt","rb")
print(file)
MODES OF FILES
IV WRITING A DATA INTO A FILE
(a) write() method
• The write method is used to write
a string to an already opened file.
• String may include members,
special characters, other symbols.
f = open('file1.txt', 'w')
#enter characters from keyboard
str = input('Enter text:')
#write the string into file
f.write(str)
#closing the file
f.close()
writelines() method
• writelines() method is used
to write a list of strings.
• f = open('file1.txt', 'w’)
lines=["hello world,","welcome to the world
of python"]
f.writelines(lines)
#closing the file
f.close()
print("data written to file")
V APPEND DATA TO FILE
append() method
• To append a file, you must open it
using ‘a’ or ‘ab’ mode depending
on whether it is a text or a binary
file.
• f = open('file1.txt', 'a’)
f.write("n my name is dev")
#closing the file
f.close()
print("data written to file")
V READ , READLINE, READLINES METHODS
(a) read() method
• This method is used to read a
string from an already opened
file.
• f = open('file1.txt', 'r’)
print(f.read(10))
f.close()
(b) readline() method
• This method is used to read a
single line from the file.
• f = open('file1.txt', 'r’)
print(f.readline())
print(f.readline())
print(f.readline())
f.close()
(c) readlines() method
• This method is used to read all
the lines in a file.
• f = open('file1.txt', 'r’)
print(f.readlines())
f.close()
VI DISPLAY THE CONTENTS OF A FILE USING LOOP
Display the contents of a file using FOR loop
• f = open('file1.txt', 'r’)
for line in f:
print(line)
f.close(
VII OPENING A FILE USING with KEYWORD
With Keyword
• with open ("file1.txt","rb") as file:
for line in file:
print(line)
file.close()
• with open ("file1.txt","r") as file:
for line in file:
print(line)
file.close()
VIII SPLITTING WORDS
split() function
• This function is used to split the
strings into words.
• with open ("file1.txt","b") as file:
line= file.readline()
words= line.split()
print(words)
IX EXCERCISE
1. What is the output of the Code?
Program 1
• Write a program that accepts filename as an input from the user.
Open the file and count the number of times a character appears in
the file
Program 2
• Write a program that reads data from a file and calculates the
percentage of vowels and consonants
Program 3
• Write a program to count number of lines, words, characters in a
text file
X RENAMING AND DELETING
rename() method
• Rename () method takes two
arguments, the current filename
and the new filename.
• Import os
os.rename(“fileo.txt”,”filen.txt”)
print(“file renamed”)
remove() method
• Remove() method is used to
delete file.
• Import os
os.remove(“file1.txt”)
print(“file deleted”)
XI PICKLE
PICKLE
• Text files are useful when we do not want to perform any
calculations on the data. What happens if we want to store some
structured data in the files? For example, we want to store some
employee details like employee identification number (int type),
name (string type) and salary (float type) in a file. This data is well
structured and got different types. To store such data, we need to
create a class Employee with the instance variables id, name and sal
as shown in next slide.
PICKLE
Pickle
• In the previous program, we create an object to class and store
actual data into that object. Later, this object should be stored into a
binary file in the form of bytes. This is called pickle or serialization.
• So, let’s understand that pickle is a process of converting a class
object into a byte stream so that it can be stored into a file. This is
also called object serialization.
Pickle
• Pickling is done using the dump() method of ‘pickle’ module as:
pickle.dump(object, file)
• The preceding statement stores the ‘object’ into the binary ‘file’.
Once the objects are stored into a file, we can read them from the
file at any time.
Pickle Implementation
Unpickle
• Unpickle is a process whereby a byte stream is converted back into a
class object. It means, unpickling represents reading the class objects
from the file.
• Unpickling is also called desearialization.
• Unpickling is done using the load() method of ‘pickle’ module as:
object = pickle.load(file)
• Here, the load() method reads an object from a binary ‘file’ and returns
it into ‘object’. Let’s remember that pickling and unpickling should be
done using binary files since they support byte streams. The word
stream represents data flow. So, byte stream represents flow of bytes.
Unpickle Implementation
Picking and Unpickling a class object
XII seek() AND tell() method
tell() method
• We know that data in the binary files is stored in the form of bytes.
When we conduct reading or writing operations on a binary file, a file
pointer moves inside the file depending on how many bytes are
written or read from the file.
• For example, if we read 10 bytes of data from a file, the file pointer
will be positioned at the 10th byte so that it is possible to continue
reading from the 11th byte onwards.
• To know the position of the file pointer, we can use the tell()
method.
•
tell() method
• It returns the current position of the file pointer from the beginning
of the file. It is used in the form: n = f.tell()
• Here, ‘f’ represents file handler or file object. ‘n’ is an integer that
represents the byte position where the file pointer is positioned. In
case, we want to move the file pointer to another position, we can
use the seek() method.
seek () method
• This method takes two arguments: f.seek(offset, fromwhere)
• Here, ‘offset’ represents how many bytes to move. ‘fromwhere’
represents from which position to move.
• For example, ‘fromwhere’ can be 0, 1 or 2. Here, 0 represents from
the beginning of the file, 1 represents from the current position and
2 represents from the ending of the file. The default value of
‘fromwhere’ is 0, i.e. beginning of the file.
seek() method
• f.seek(10) #same as f.seek(10, 0)
• This will move the file pointer to the 11th byte (i.e. 10+1) from the
beginning of the file (0 represents beginning of the file). So, any
reading operation will read data from 11th byte onwards.
• f.seek(-10, 2)
• This will move the file pointer to the 9th byte (-10+1) from the ending
of the file (2 represents ending of the file). The negative sign before
10 represents moving back in the file.
XIII RANDOM ACCESSING OF BINARY FILES USING
mmap
mmap
• mmap – ‘memory mapped file’ is a module in Python that is useful to
map or link to a binary file and manipulate the data of the file as we
do with the strings.
• It means, once a binary file is created with some data, that data is
viewed as strings and can be manipulated using mmap module. The
first step to use the mmap module is to map the file using the
mmap() method as:
mm = mmap.mmap(f.fileno(), 0)
• This will map the currently opened file (i.e. ‘f’) with the file object
‘mm
• Please observe the arguments of mmap() method.
• The first argument is ‘f.fileno()’ .
• This indicates that fileno() is a handle to the file object ‘f’.
• This ‘f’ represents the actual binary file that is being mapped. The second
argument is zero( 0) represents the total size of the file should be
considered for mapping. So, the entire file represented by the file object
‘f’ is mapped in memory to the object ‘mm’. This means, ‘mm’ will now
onwards behave like the file ‘f’.
mmap
• Now, we can read the data from the file using read() or readline()
methods as:
print(mm.read()) #displays entire file data
print(mm.readline()) #displays the first line of the file
mmap
• We can retrieve data from the file using slicing operator as:
print(mm[5:]) #display from 5th byte till the end
print(mm[5:10]) #display from 5th to 9th bytes
• It is also possible to modify or replace the data of the file using slicing
as:
mm[5:10] = str #replace from 5th to 9th characters by string ‘str’
mmap
• We can also use find() method that returns the first position of a string in
the file as:
n = mm.find(name)
#return the position of name in the file
• We can also use seek() method to position the file pointer to any
position we want as:
mm.seek(10, 0)
#position the file pointer to 10th byte from beginning of file
XIV ZIPPING AND UNZIPPING OF FILES
File Handling Btech computer science and engineering ppt
ZIPPING AND UNZIPPING OF FILES
• We know that some softwares like ‘winzip’ provide zipping and
unzipping of file data.
• In zipping the file contents, following two things could happen:
 The file contents are compressed and hence the size will be reduced.
The format of data will be changed making it unreadable.
ZIPPING AND UNZIPPING OF FILES
• While zipping a file content, a zipping algorithm (logic) is used in such
a way that the algorithm first finds out which bit pattern is most
often repeated in the original file and replaces that bit pattern with a
0. Then the algorithm searches for the next bit pattern which is most
often repeated in the input file.
• In its place, a 1 is substituted. The third repeated bit pattern will be
replaced by 10, the fourth by 11, the fifth by 100, and so on. In this
way, the original bit patterns are replaced by lesser number of bits.
This file with lesser number of bits is called ‘zipped file’ or
‘compressed file’.
ZIPPING AND UNZIPPING OF FILES
• In Python, the module zipfile contains ZipFile class that helps us to zip
or unzip a file contents. For example, to zip the files, we should first
pass the zip file name in write mode with an attribute ZIP_DEFLATED
to the ZipFile class object as:
f = ZipFile('test.zip', 'w', ZIP_DEFLATED)
• Here, ‘f’ is the ZipFile class object to which test.zip file name is
passed. This is the zip file that is created finally. The next step is to
add the filenames that are to be zipped, using write() method as:
f.write('file1.txt’)
f.write('file2.txt')
Python program to compress the contents of files
extractall() method
• In the previous program, we assumed that the three files: file1.txt, file2.txt
and file3.txt are already available in the current directory where this
program is run.
• To unzip the contents of the compressed files and get back their original
contents, we can use ZipFile class object in read mode as:
z = ZipFile('test.zip', 'r’)
• Here, test.zip is the filename that contains the compressed files.
• To extract all the files from the zip file object ‘z’, we can use the
extractall() method as:
z.extractall()
Python program to unzip the contents of the files
that are available in a zip file

More Related Content

PPTX
File handling in Python
PDF
Python programming : Files
PPTX
file handling in python using exception statement
PPTX
01 file handling for class use class pptx
PPTX
1 cs xii_python_file_handling text n binary file
PPTX
Binary File.pptx
PPTX
DFH PDF-converted.pptx
PPTX
File Handling in Python -binary files.pptx
File handling in Python
Python programming : Files
file handling in python using exception statement
01 file handling for class use class pptx
1 cs xii_python_file_handling text n binary file
Binary File.pptx
DFH PDF-converted.pptx
File Handling in Python -binary files.pptx

Similar to File Handling Btech computer science and engineering ppt (20)

PDF
File handling with python class 12th .pdf
PPTX
Data File Handling in Python Programming
PPTX
Chapter 08 data file handling
PPTX
File Handling Python
PPTX
files.pptx
PPTX
Unit V.pptx
PPTX
File Operations in python Read ,Write,binary file etc.
PPTX
FILE HANDLING COMPUTER SCIENCE -FILES.pptx
PDF
lecs102.pdf kjolholhkl';l;llkklkhjhjbhjjmnj
PPTX
File handling & basic operation on file in python
PDF
File handling3 (1).pdf uhgipughserigrfiogrehpiuhnfi;reuge
PPTX
pspp-rsk.pptx
PPTX
Python data file handling
DOCX
File Handling in python.docx
PPTX
FILE HANDLING IN PYTHON Presentation Computer Science
PPTX
file handling.pptx avlothaan pa thambi popa
PPTX
Chapter - 5.pptx
PPTX
FILE HANDLING.pptx
PDF
Python Programming - XII. File Processing
PDF
5.1 Binary File Handling.pdf
File handling with python class 12th .pdf
Data File Handling in Python Programming
Chapter 08 data file handling
File Handling Python
files.pptx
Unit V.pptx
File Operations in python Read ,Write,binary file etc.
FILE HANDLING COMPUTER SCIENCE -FILES.pptx
lecs102.pdf kjolholhkl';l;llkklkhjhjbhjjmnj
File handling & basic operation on file in python
File handling3 (1).pdf uhgipughserigrfiogrehpiuhnfi;reuge
pspp-rsk.pptx
Python data file handling
File Handling in python.docx
FILE HANDLING IN PYTHON Presentation Computer Science
file handling.pptx avlothaan pa thambi popa
Chapter - 5.pptx
FILE HANDLING.pptx
Python Programming - XII. File Processing
5.1 Binary File Handling.pdf
Ad

Recently uploaded (20)

PPT
Introduction, IoT Design Methodology, Case Study on IoT System for Weather Mo...
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PPT
Mechanical Engineering MATERIALS Selection
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PPTX
additive manufacturing of ss316l using mig welding
PDF
PPT on Performance Review to get promotions
PPTX
Internet of Things (IOT) - A guide to understanding
PPTX
Lecture Notes Electrical Wiring System Components
PPTX
Geodesy 1.pptx...............................................
PPT
Project quality management in manufacturing
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PPTX
bas. eng. economics group 4 presentation 1.pptx
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PDF
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
PDF
R24 SURVEYING LAB MANUAL for civil enggi
PPTX
Artificial Intelligence
Introduction, IoT Design Methodology, Case Study on IoT System for Weather Mo...
Operating System & Kernel Study Guide-1 - converted.pdf
Mechanical Engineering MATERIALS Selection
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
additive manufacturing of ss316l using mig welding
PPT on Performance Review to get promotions
Internet of Things (IOT) - A guide to understanding
Lecture Notes Electrical Wiring System Components
Geodesy 1.pptx...............................................
Project quality management in manufacturing
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
bas. eng. economics group 4 presentation 1.pptx
Embodied AI: Ushering in the Next Era of Intelligent Systems
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
R24 SURVEYING LAB MANUAL for civil enggi
Artificial Intelligence
Ad

File Handling Btech computer science and engineering ppt

  • 4. FILES • Data is very important. Every organization depends on its data for continuing its business operations. If the data is lost, the organization has to be closed. • This is the reason computers are primarily created for handling data, especially for storing and retrieving data. In later days, programs are developed to process the data that is stored in the computer.
  • 5. FILES • To store data in a computer, we need files. For example, we can store employee data like employee number, name and salary in a file in the computer and later use it whenever we want. • Similarly, we can store student data like student roll number, name and marks in the computer. In computers’ view, a file is nothing but collection of data that is available to a program. Once we store data in a computer file, we can retrieve it and use it depending on our requirements. •
  • 7. ADVANTAGES OF STORING A DATA IN A FILE • When the data is stored in a file, it is stored permanently. This means that even though the computer is switched off, the data is not removed from the memory since the file is stored on hard disk or CD. This file data can be utilized later, whenever required. • It is possible to update the file data. For example, we can add new data to the existing file, delete unnecessary data from the file and modify the available data of the file. This makes the file more useful.
  • 8. ADVANTAGES OF STORING A DATA IN A FILE • Once the data is stored in a file, the same data can be shared by various programs. For example, once employee data is stored in a file, it can be used in a program to calculate employees’ net salaries or in another program to calculate income tax payable by the employees. • Files are highly useful to store huge amount of data. For example, voters’ list or census data.
  • 9. II TYPES OF FILES
  • 10. TYPES OF FILES • In Python, there are two types of files. • They are:  Text files Binary files • Text files store the data in the form of characters. For example, if we store employee name “Ganesh”, it will be stored as 6 characters and the employee salary 8900.75 is stored as 7 characters. • Text files are used to store characters or strings.
  • 11. TYPES OF FILES • Binary files store entire data in the form of bytes, i.e. a group of 8 bits each. For example, a character is stored as a byte and an integer is stored in the form of 8 bytes (on a 64 bit machine). When the data is retrieved from the binary file, the programmer can retrieve the data as bytes. • Binary files can be used to store text, images, audio and video. Image files are generally available in .jpg, .gif or .png formats. • We cannot use text files to store images as the images do not contain characters.
  • 12. TYPES OF FILES • On the other hand, images contain pixels which are minute dots with which the picture is composed of. • Each pixel can be represented by a bit, i.e. either 1 or 0. Since these bits can be handled by binary files, we can say that they are highly suitable to store images. It is very important to know how to create files, store data in the files and retrieve the data from the files in Python
  • 13. III READ A FILE
  • 14. OPENING A FILE • file = open("file1.txt","rb") print(file)
  • 16. IV WRITING A DATA INTO A FILE
  • 17. (a) write() method • The write method is used to write a string to an already opened file. • String may include members, special characters, other symbols. f = open('file1.txt', 'w') #enter characters from keyboard str = input('Enter text:') #write the string into file f.write(str) #closing the file f.close()
  • 18. writelines() method • writelines() method is used to write a list of strings. • f = open('file1.txt', 'w’) lines=["hello world,","welcome to the world of python"] f.writelines(lines) #closing the file f.close() print("data written to file")
  • 19. V APPEND DATA TO FILE
  • 20. append() method • To append a file, you must open it using ‘a’ or ‘ab’ mode depending on whether it is a text or a binary file. • f = open('file1.txt', 'a’) f.write("n my name is dev") #closing the file f.close() print("data written to file")
  • 21. V READ , READLINE, READLINES METHODS
  • 22. (a) read() method • This method is used to read a string from an already opened file. • f = open('file1.txt', 'r’) print(f.read(10)) f.close()
  • 23. (b) readline() method • This method is used to read a single line from the file. • f = open('file1.txt', 'r’) print(f.readline()) print(f.readline()) print(f.readline()) f.close()
  • 24. (c) readlines() method • This method is used to read all the lines in a file. • f = open('file1.txt', 'r’) print(f.readlines()) f.close()
  • 25. VI DISPLAY THE CONTENTS OF A FILE USING LOOP
  • 26. Display the contents of a file using FOR loop • f = open('file1.txt', 'r’) for line in f: print(line) f.close(
  • 27. VII OPENING A FILE USING with KEYWORD
  • 28. With Keyword • with open ("file1.txt","rb") as file: for line in file: print(line) file.close() • with open ("file1.txt","r") as file: for line in file: print(line) file.close()
  • 30. split() function • This function is used to split the strings into words. • with open ("file1.txt","b") as file: line= file.readline() words= line.split() print(words)
  • 32. 1. What is the output of the Code?
  • 33. Program 1 • Write a program that accepts filename as an input from the user. Open the file and count the number of times a character appears in the file
  • 34. Program 2 • Write a program that reads data from a file and calculates the percentage of vowels and consonants
  • 35. Program 3 • Write a program to count number of lines, words, characters in a text file
  • 36. X RENAMING AND DELETING
  • 37. rename() method • Rename () method takes two arguments, the current filename and the new filename. • Import os os.rename(“fileo.txt”,”filen.txt”) print(“file renamed”)
  • 38. remove() method • Remove() method is used to delete file. • Import os os.remove(“file1.txt”) print(“file deleted”)
  • 40. PICKLE • Text files are useful when we do not want to perform any calculations on the data. What happens if we want to store some structured data in the files? For example, we want to store some employee details like employee identification number (int type), name (string type) and salary (float type) in a file. This data is well structured and got different types. To store such data, we need to create a class Employee with the instance variables id, name and sal as shown in next slide.
  • 42. Pickle • In the previous program, we create an object to class and store actual data into that object. Later, this object should be stored into a binary file in the form of bytes. This is called pickle or serialization. • So, let’s understand that pickle is a process of converting a class object into a byte stream so that it can be stored into a file. This is also called object serialization.
  • 43. Pickle • Pickling is done using the dump() method of ‘pickle’ module as: pickle.dump(object, file) • The preceding statement stores the ‘object’ into the binary ‘file’. Once the objects are stored into a file, we can read them from the file at any time.
  • 45. Unpickle • Unpickle is a process whereby a byte stream is converted back into a class object. It means, unpickling represents reading the class objects from the file. • Unpickling is also called desearialization. • Unpickling is done using the load() method of ‘pickle’ module as: object = pickle.load(file) • Here, the load() method reads an object from a binary ‘file’ and returns it into ‘object’. Let’s remember that pickling and unpickling should be done using binary files since they support byte streams. The word stream represents data flow. So, byte stream represents flow of bytes.
  • 47. Picking and Unpickling a class object
  • 48. XII seek() AND tell() method
  • 49. tell() method • We know that data in the binary files is stored in the form of bytes. When we conduct reading or writing operations on a binary file, a file pointer moves inside the file depending on how many bytes are written or read from the file. • For example, if we read 10 bytes of data from a file, the file pointer will be positioned at the 10th byte so that it is possible to continue reading from the 11th byte onwards. • To know the position of the file pointer, we can use the tell() method. •
  • 50. tell() method • It returns the current position of the file pointer from the beginning of the file. It is used in the form: n = f.tell() • Here, ‘f’ represents file handler or file object. ‘n’ is an integer that represents the byte position where the file pointer is positioned. In case, we want to move the file pointer to another position, we can use the seek() method.
  • 51. seek () method • This method takes two arguments: f.seek(offset, fromwhere) • Here, ‘offset’ represents how many bytes to move. ‘fromwhere’ represents from which position to move. • For example, ‘fromwhere’ can be 0, 1 or 2. Here, 0 represents from the beginning of the file, 1 represents from the current position and 2 represents from the ending of the file. The default value of ‘fromwhere’ is 0, i.e. beginning of the file.
  • 52. seek() method • f.seek(10) #same as f.seek(10, 0) • This will move the file pointer to the 11th byte (i.e. 10+1) from the beginning of the file (0 represents beginning of the file). So, any reading operation will read data from 11th byte onwards. • f.seek(-10, 2) • This will move the file pointer to the 9th byte (-10+1) from the ending of the file (2 represents ending of the file). The negative sign before 10 represents moving back in the file.
  • 53. XIII RANDOM ACCESSING OF BINARY FILES USING mmap
  • 54. mmap • mmap – ‘memory mapped file’ is a module in Python that is useful to map or link to a binary file and manipulate the data of the file as we do with the strings. • It means, once a binary file is created with some data, that data is viewed as strings and can be manipulated using mmap module. The first step to use the mmap module is to map the file using the mmap() method as: mm = mmap.mmap(f.fileno(), 0) • This will map the currently opened file (i.e. ‘f’) with the file object ‘mm
  • 55. • Please observe the arguments of mmap() method. • The first argument is ‘f.fileno()’ . • This indicates that fileno() is a handle to the file object ‘f’. • This ‘f’ represents the actual binary file that is being mapped. The second argument is zero( 0) represents the total size of the file should be considered for mapping. So, the entire file represented by the file object ‘f’ is mapped in memory to the object ‘mm’. This means, ‘mm’ will now onwards behave like the file ‘f’.
  • 56. mmap • Now, we can read the data from the file using read() or readline() methods as: print(mm.read()) #displays entire file data print(mm.readline()) #displays the first line of the file
  • 57. mmap • We can retrieve data from the file using slicing operator as: print(mm[5:]) #display from 5th byte till the end print(mm[5:10]) #display from 5th to 9th bytes • It is also possible to modify or replace the data of the file using slicing as: mm[5:10] = str #replace from 5th to 9th characters by string ‘str’
  • 58. mmap • We can also use find() method that returns the first position of a string in the file as: n = mm.find(name) #return the position of name in the file • We can also use seek() method to position the file pointer to any position we want as: mm.seek(10, 0) #position the file pointer to 10th byte from beginning of file
  • 59. XIV ZIPPING AND UNZIPPING OF FILES
  • 61. ZIPPING AND UNZIPPING OF FILES • We know that some softwares like ‘winzip’ provide zipping and unzipping of file data. • In zipping the file contents, following two things could happen:  The file contents are compressed and hence the size will be reduced. The format of data will be changed making it unreadable.
  • 62. ZIPPING AND UNZIPPING OF FILES • While zipping a file content, a zipping algorithm (logic) is used in such a way that the algorithm first finds out which bit pattern is most often repeated in the original file and replaces that bit pattern with a 0. Then the algorithm searches for the next bit pattern which is most often repeated in the input file. • In its place, a 1 is substituted. The third repeated bit pattern will be replaced by 10, the fourth by 11, the fifth by 100, and so on. In this way, the original bit patterns are replaced by lesser number of bits. This file with lesser number of bits is called ‘zipped file’ or ‘compressed file’.
  • 63. ZIPPING AND UNZIPPING OF FILES • In Python, the module zipfile contains ZipFile class that helps us to zip or unzip a file contents. For example, to zip the files, we should first pass the zip file name in write mode with an attribute ZIP_DEFLATED to the ZipFile class object as: f = ZipFile('test.zip', 'w', ZIP_DEFLATED) • Here, ‘f’ is the ZipFile class object to which test.zip file name is passed. This is the zip file that is created finally. The next step is to add the filenames that are to be zipped, using write() method as: f.write('file1.txt’) f.write('file2.txt')
  • 64. Python program to compress the contents of files
  • 65. extractall() method • In the previous program, we assumed that the three files: file1.txt, file2.txt and file3.txt are already available in the current directory where this program is run. • To unzip the contents of the compressed files and get back their original contents, we can use ZipFile class object in read mode as: z = ZipFile('test.zip', 'r’) • Here, test.zip is the filename that contains the compressed files. • To extract all the files from the zip file object ‘z’, we can use the extractall() method as: z.extractall()
  • 66. Python program to unzip the contents of the files that are available in a zip file