0% found this document useful (0 votes)
0 views

Chapter 2-File Handling in Python

Uploaded by

Prerith.M
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Chapter 2-File Handling in Python

Uploaded by

Prerith.M
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

CHAPTER 2

FILE HANDLING IN PYTHON


FILE: A file is a named location on a secondary storage media where data are permanently
stored for later access.
TYPES OF FILES
There are mainly two types of data files:
1. Text file.
2. Binary file.

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:

 Creating and opening a file


 Writing data in a file NOTE: Python has the io
module that contains
 Traversing a file
different functions for
 Reading data from a file handling files.

Opening a file
To open a file in Python, we use the open() function

Syntax: file_object = open(file_name, access_mode)


file_object= open(file_name, access_mode)

file_object= open(file_name, access_mode)


file_object: A file_object establishes a link between
file_object= open(file_name, the program and the data file stored in
access_mode)
the permanent storage.

Program File object

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> Returns the name of the file.

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.

Example: myobject = open(“myfile.txt”, “a+”)


file_object= open(file_name, access_mode)
Closing a file file_object= open(file_name, access_mode)
Once we are done with the read/write operations on
file_object= open(file_name, a file, it is a good practice to close the
access_mode)
file. Python provides a close() method to do so.
While closing a file, the system frees the memory allocated to it.

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.

Syntax: with open(file_name, access_mode) as file_object:


with open (file_name, access_mode) as file_ object:
The advantage of using with clause is that any file that is opened using with clause is
file_object= open(file_name, access_mode)
closed automatically, once the control comes outside the with clause.
file_object= open(file_name, access_mode)
In case the user forgets to close the file explicitly or if an exception occurs, the file is closed
automatically. Also, it providesfile_object=
a simpler syntax.
open(file_name, access_mode)

Example: with open(“myfile.txt”, “r”) as myobject:


Content = myobject.read()

file_object= open(file_name, access_mode)

file_object= open(file_name, access_mode)

file_object= open(file_name, access_mode)


WRITING TO A TEXT FILE
If we open an existing file in write mode (‘w’), the previous data will be erased, and the file
object will be positioned at the beginning of the file.
On the other hand, in append mode (‘a’), new data will be added at the end of the previous
data as the file object is at the end of the file.
After opening the file, we can use the following methods to write data in the file.
• write() - for writing a single string
• writelines() - for writing a sequence of strings
1. The write() method
write() method takes a string as an argument and writes it to the text file.

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.

>>> myobject = open(“myfile.txt”, ‘w’)


>>>myonject.write(“Hey I have started using files
in python\n”)
Returns 41
>>>myobject.close( )

On execution, write() returns the number of characters written on to the file.

Note: ‘\n’ is treated as a singlefile_object=


character open(file_name, access_mode)

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:

Difference between write() and writelines()


Write() Writelines()
Writes single string. Writes sequence of strings.
Return number of characters being written Does not Return number of characters
in the file. being written in the file.
Takes string as an argument and writes it Writes each element of list as a line in the
to the text file. file.
Numbers should be converted into string Numbers are not converted into string.
suing str().

READING FROM THE TEXT FILE

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.

There are three ways to read the contents of a file:


 read()
 readline([n])
 readlines()

1. The read() method


This method is used to read a specified number of bytes of data from a data file.

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()

2. The readline([n]) method


This method reads one complete line from a file where each line terminates with a newline
(\n) character.
It can also be used to read a specified number (n) of bytes of data from a file but maximum
up to the newline character (\n).

Ex: >>> myobject=open("myfile.txt",'r')


>>> myobject.readline(10)
'Hello ever'
>>> myobject.close()

If no argument or a negative number is specified, it reads a complete line and returns


string.
Ex: >>>myobject=open("myfile.txt",'r')
>>> print (myobject.readline())
'Hello everyone\n'
>>> myobject.close()
Note: To read the entire file line by line using the readline(), we can use a loop. This process is
known as looping/ iterating over a file object.

3. The readlines() method


The method reads all the lines and returns the lines along with newline as a list of strings.

Ex: >>> myobject=open("myfile.txt", 'r')


>>> print(myobject.readlines())
['Hello everyone\n', 'Writing multiline
strings\n', 'This is the third line']
>>> myobject.close()
As shown in the above output, when we read a file using readlines() function, lines in the
file become members of a list, where each list element ends with a newline character (‘\n’).

In case we want to display each word of a line separately as an element of a list, then we
can use split() function.

Ex: >>> myobject=open("myfile.txt",'r')


>>> d=myobject.readlines()
>>> for line in d:
words=line.split()
print(words)
['Hello', 'everyone']
['Writing', 'multiline', 'strings']
['This', 'is', 'the', 'third', 'line']

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:

SETTING OFFSETS IN A FILE


The functions that we have learnt till now are used to access the data sequentially from a
file. But if we want to access data in a random fashion, then Python gives us seek() and
tell() functions to do so.

a) The tell() method


This function returns an integer that specifies the current position of the file object in the
file. The position specified is the byte position from the beginning of the file till the current
position of the file object.

Syntax: file_object.tell()

b) The seek() method


This method is used to position the file object at a particular position in a file.

Syntax: file_object.seek(offset [, reference_point])


Offset: is the number of bytes by which the file object is to be moved.
reference_point: indicates the starting position of the file object. That is, with reference
to which position, the offset has to be counted.
Reference_point can have any of the following values:
0 - beginning of the file
1 - current position of the file
2 - end of file

Note: By default, the value of reference_point is 0

Program to perform tell() and seek()

Output:
CREATING AND TRAVERSING THE TEXT FILE

a) Creating a file and writing data


 To create a text file, we use the open() method and provide the filename and the mode.
 If the file already exists with the same name, the open() function will behave differently
depending on the mode (write or append) used.
 If it is in write mode (w), then all the existing contents of file will be lost, and an empty
file will be created with the same name.
 If the file is created in append mode (a), then the new data will be written after the
existing data.
 In both cases, if the file does not exist, then a new empty file will be created.

Program to create a text file and write data in it

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.

The dump() method

 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.

Syntax: dump(data_object, file_object)

Example:
import pickle
listvalues=[1,"Geetika",'F', 26]
fileobject=open("mybinary.dat", "wb")
pickle.dump(listvalues,fileobject)
fileobject.close()

The load() method


 This method is used to load (unpickling) data from a binary file.
 The file to be loaded is opened in binary read (rb) mode.

Syntax: StoSre_object = load(file_object)


Example:
import pickle
print("The data that were stored in file are: ")
fileobject=open("mybinary.dat","rb")
objectvar=pickle.load(fileobject)
fileobject.close()
print(objectvar)
Output:
>>>
RESTART: Path_to_file\Program2-7.py
The data that were stored in file are:
[1, 'Geetika', 'F', 26]
>>>

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy