Chapter 2-File Handling in Python
Chapter 2-File Handling in Python
Text file
A text file consists of human readable characters, which can be opened by any text
editor.
Example of text file : .txt, .py, .csv etc
A text file can be understood as a sequence of characters consisting of alphabets,
numbers and other special symbols.
The value of each character of the text file is stored in terms of bytes (0’s and 1’s),
which represents ASCII, UNICODE or any other encoding scheme.
Each line of a text file is terminated by a special character, called the End of Line
(EOL).
The default EOL character in Python is the newline (\n).
Whitespace, comma (,) and tab (\t) are also commonly used to separate values in a
text file.
Binary Files
Binary files are made up of non-human readable characters and symbols.
Example of binary file: image, audio, video etc
We need specific software/program to read or write the contents of a binary file.
Binary files are also stored in terms of bytes (0s and 1s), but these bytes do not
represent the ASCII values of characters.
Trying to open a binary file using a text editor will show some garbage values.
It is difficult to remove any error which may occur in the binary file as their stored
contents are not human readable.
Text File Binary File
Consists of human readable characters. Consists of non-human readable
characters.
Can be opened by any text editor. need specific software/program to read
or write the contents.
Translator is needed. No need of translator.
Each value of character is stored as Each value of character is stored as
bytes, which are represented by ASCII, bytes, but not represented by ASCII
UNICODE or any encoding scheme. value of character.
Each line of a text file is terminated by Not terminated.
a special character, called the End of
Line (EOL).
Example : .txt, .py, .csv etc Example : image, audio, video etc
OPENING AND CLOSING A FILE
In real world applications, computer programs deal with data coming from different sources
like databases, CSV files, HTML, XML, JSON, etc. We broadly access files either to write or
read data from it.
Some of the operations performed on files:
Opening a file
To open a file in Python, we use the open() function
data file
We can use this variable to transfer data to and from the file (read and write) by calling the
functions defined in the Python’s io module.
If the file does not exist, the above statement creates a new empty file and assigns it the
name we specify in the statement.
The file_object has certain attributes that tells us basic information about the file, such as:
Attributes Meaning
<file.closed> Returns true if the file is closed and false otherwise.
<file.mode> Returns the access mode in which the file was opened.
file_name: The file_name should be the name of the file that has to be opened. If the file is
not in the current working directory, then we need to specify the complete path of the file
along with its name.
access_mode: The access_mode is an optional argument that represents the mode in
which the file has to be accessed by the program. It is also referred to as processing mode.
File mode Description File offset position
<r> Opens the file in read-only mode. Beginning of the file
<rb> Opens the file in binary and read-only mode. Beginning of the file
<r+> or <+r> Opens the file in both read and write mode. Beginning of the file
<w> Opens the file in write mode. If the file already Beginning of the file
exists, all the contents will be overwritten. If the
file doesn’t exist, then a new file will be created.
<wb+> or Opens the file in read, write and binary mode. If Beginning of the file
<+wb> the file already exists, all the contents will be
overwritten. If the file doesn’t exist, then a new file
will be created.
<a> Opens the file in append mode. If the file doesn’t End of the file
exist, then a new file will be created.
<a+> or <+a> Opens the file in append and read mode. If the file End of the file
doesn’t exist, then a new file will be created.
Syntax: file_object.close( )
file_object= open(file_name,
Python makes sure that any unwrittenaccess_mode)
or unsaved data is flushed off (written) to the file
before it is closed. Hence, it is always advised to close the file once our work is done. Also,
file_object= open(file_name,
if the file object is re-assigned to someaccess_mode)
other file, the previous file is automatically closed.
Opening a file using with clause
file_object= open(file_name,
access_mode)
In Python, we can also open a file using with clause.
It returns the number of characters being written on single execution of the write() method.
Also, we need to add a newline character (\n) at the end of every sentence to mark the end
of line.
file_object=
If numeric data are to be written open(file_name,
to a text access_mode)
file, the data need to be converted into string
before writing to the file. file_object= open(file_name, access_mode)
>>>myobject=open("myfile.txt",'w')
>>> marks=58
#number 58 is converted to a string using #str()
>>> myobject.write(str(marks))
Returns 2
>>>myobject.close()
The write() actually writes data onto a buffer. When the close() method is executed, the
contents from this buffer are moved to the file located on the permanent storage.
2. The writelines() method
This method is used to write multiple strings to a file. We need to pass an iterable object
like lists, tuple, etc.
Unlike write(), the writelines() method does not return the number of characters written in
the file.
>>> myobject=open("myfile.txt",'w')
>>> lines = ["Hello everyone\n", "Writing multiline
strings\n", "This is the third line"]
>>> myobject.writelines(lines)
>>>myobject.close()
Output:
We can write a program to read the contents of a file. Before reading a file, we must make
sure that the file is opened in “r”, “r+”, “w+” or “a+” mode.
Syntax: file_object.read(n)
Ex:with>>>myobject=open("myfile.txt",'r')
open (file_name, access_mode)
as file_ object:
>>> myobject.read(10)
'Hello ever'
file_object= open(file_name, access_mode)
>>> myobject.close()
file_object= open(file_name, access_mode)
If no argument or a negative number is specified in read(), the entire file content is read.
file_object= open(file_name, access_mode)
Ex: >>> myobject=open("myfile.txt",'r')
>>> print(myobject.read())
Hello everyone
Writing multiline strings
This is the third line
>>> myobject.close()
In case we want to display each word of a line separately as an element of a list, then we
can use split() function.
In the output, each string is returned as elements of a list. However, if splitlines() is used
instead of split(), then each line is returned as element of a list.
Ex: >>> myobject=open("myfile.txt",'r')
>>> d=myobject.readlines()
>>> for line in d:
words=line.splitlines()
print(words)
['Hello everyone']
['Writing multiline strings']
['This is the third line']
Program to write and read to a text file.
Output:
Syntax: file_object.tell()
Output:
CREATING AND TRAVERSING THE TEXT FILE
Output:
b) Traversing a file and displaying data
To read and display data that is stored in a text file.
Program to Travers and display a data in a text file.
Output:
Program to perform reading and writing operation in a text file
Output:
THE PICKLE MODULE
Python considers everything as an object. So, all data types including list, tuple, dictionary,
etc. are also considered as objects.
To save any object structure along with data, Python provides a module called Pickle.
During execution of a program, we may require to store current state of variables so that
we can retrieve them later to its present state.
The module Pickle is used for serializing and de-serializing any Python object structure.
Serialization:
Serialization process is also called pickling.
It is the process of transforming data or an object in memory (RAM) to a stream of
bytes called byte streams.
These byte streams in a binary file can then be stored in a disk or in a database or
sent through a network.
De-serialization:
De-serialization process is als0 called unpickling.
It is the inverse of pickling process where a byte stream is converted back to Python
object.
The pickle module provides two methods - dump() and load() to work with binary files for
pickling and unpickling, respectively.
This method is used to convert (pickling) Python objects for writing data in a binary
file.
The file in which data are to be dumped, needs to be opened in binary write (wb)
mode.
Example:
import pickle
listvalues=[1,"Geetika",'F', 26]
fileobject=open("mybinary.dat", "wb")
pickle.dump(listvalues,fileobject)
fileobject.close()