You are on page 1of 16

Data file handling

If we want to use file in python program to store data in file or read


data from file then it is considered as Data file handling.

Why we use file?

During execution of a program we take some input, make some


calculation and print the result but as soon as the program execution
is over we are not able to access that data again because it was
stored in primary memory , to store data permanently we use file
which stores in secondary memory so that we can use data anytime
again .

Data file handling take place in the following order

Close
Open Process data
file
file
(read/write
data)

Types of file

*Text File:-

A text file is considered as sequence of lines. A line is


considered as group of ASCII(American standard code for
information interchange) or Unicode(universal code). By default
data stored in ASCII FORMAT. In text file each line is ended with END
OF LINE(EOL) . In python ‘\n’ is known as EOL character. Text files
are stored in human readable form.
*Binary File:-

In binary file data is stored in the computer understandable


form(binary ) , we use binary file to store audio, video and image file.
We also use binary file to store numbers.

In binary file there is no end of line character(EOL). Data is already


stored in computer understandable form hence data conversion is
not required hence read/write operation in binary file are faster as
compared to text file.
How to open a file in python program?

File can be open in python program using open() function. Open


function takes two strings as arguments.

F=open(“name of file”,”mode”)

It indicates the purpose of opening file i.e.


reading or writting

Indicates name of the file which you want to


open

F is considered as file handler variable. Once a file is


opened cannot be accessed by name but referred by file
handler variable
Mode Modes & Description

r
Opens a file for reading only. The file pointer is placed at the
beginning of the file. This is the default mode.

rb Opens a file for reading only in binary format. The file pointer is
placed at the beginning of the file. This is the default mode.

r+ Opens a file for both reading and writing. The file pointer placed at
the beginning of the file.

rb+ Opens a file for both reading and writing in binary format. The file
pointer placed at the beginning of the file.

w Opens a file for writing only. Overwrites the file if the file exists. If
the file does not exist, creates a new file for writing.

wb Opens a file for writing only in binary format. Overwrites the file if
the file exists. If the file does not exist, creates a new file for writing.

w+ Opens a file for both writing and reading. Overwrites the existing
file if the file exists. If the file does not exist, creates a new file for
reading and writing.

wb+

Opens a file for both writing and reading in binary format.


Overwrites the existing file if the file exists. If the file does not exist,
creates a new file for reading and writing.

a Opens a file for appending. The file pointer is at the end of the file if
the file exists. That is, the file is in the append mode. If the file does
not exist, it creates a new file for writing.
ab

Opens a file for appending in binary format. The file pointer is at the
end of the file if the file exists. That is, the file is in the append
mode. If the file does not exist, it creates a new file for writing.

a+ Opens a file for both appending and reading. The file pointer is at
the end of the file if the file exists. The file opens in the append
mode. If the file does not exist, it creates a new file for reading and
writing.

ab+

Opens a file for both appending and reading in binary format. The
file pointer is at the end of the file if the file exists. The file opens in
the append mode. If the file does not exist, it creates a new file for
reading and writing.

Example:-

In the above example we tried to open a which not exist, mode is not
given in open() hence by default mode is “r”. In “r” mode file can be
open if and only if exists otherwise it will cause error that we can see
in the above example.

Suppose we have file “abc.txt”


Now if we try to open file in “r” mode it will not cause any error.

You can see >>> in next line of open statement that means there is
no error in open statement. Statement executed successfully and file
is linked with file handler variable(f) and now we can read from file.

File in ‘w’ mode

In the above example we open a file in ‘w’ mode, if file exist file will
be overwrite and if not exists then create a new file but not cause
any error.

Note : In ‘r’,’rb’,’r+’,’rb+’ file will be open if and only if file exist


otherwise cause error.

How to close file?

Once our read /write operation is over we should close file using
close().

Syntax:

File handler object.close()


Example:-

f.close()

in the above statement f is the file handler variable.

How to write in text file?

In text file we can use following functions to write:-

i) write() it is used to write group of character in a file. It


return a number which indicates number of characters
written by write() .
ii) writelines(): It is used to write multiple lines in one go using
sequence(list,tuple)

Example of write()

If you want to check whether xiic xiid written in the file or not
then you can check it by opening the file .
You can check in xyz.txt xiicxiid is there in the file

If you want to write xiic and xiid in separate lines then you have to
give’\n’ at the end of each string
After executing above code xyz.txt will contains following data.

Note : xyz.txt file’s old data will be erased from the file only data
written by above code will remains in file.

You can see xiic and xiid stored in separate line as we used ‘\n’ in
write().’\n’ is used for new line.

Can we write data in file without overwriting existing data?

Yes we can write new data in file with erasing previous data using
‘a’,’a+’,’ab’ and ’ab+’ mode

Consider the following code.

After executing code xyz.txt have following data


Example of writelines()

After executing the above code if you open subject.txt you will find
following content.
How to read from text file?

In python we have 3 functions to read from text file.

i) read() it returns a specific size string , if size is passed as


argument from the cursor position of file, if size is not pass
as argument then it read and return the remaining data from
the current position of file cursor.
ii) readline() it reads only 1 line of data at a time from file
cursor position and return line in the form of string.
iii) readlines() it reads all lines starting from file cursor position
to end of the file and return lines in the form list of string

To give example of read(),readline() and readlines() function I am


using subject.txt file which I created in the example of writelines()

read()

syntax:-

string_name=file_handler_object.read(size)

size is optional in read statement

4 is given as size that indicates it will


returns 4 character long string from file. it
reads from the beginning of the file as
when we open file in read mode cursor is
always in the beginning of file
When we execute the above code following output will be displayed

read() without size as argument

Size is not given , hence read() function will


read complete file from beginning to end
and to store in s

Output of the above code:-


readline()

Syntax of readline():-

Sting_name=file_handler_object.readline()

Example :-

Output of the code:-


If need read all line from file one by one then we can use loop as we
don’t know exactly how many lines are there in file.

While s means loop will execute till s is


having data. In the beginning I assigned
a space character in s. while s condition
is true as s is not empty body of loop
will executes. One by one all lines will
be read and print on screen. When all
lines are read from file then (None will
assign to s)i.e. s become empty and
while condition turns to false.

Output of the program

Can you notice the gap between lines


it is due to ‘\n’ character which stored
as last character of each line in
subject.txt and another’\n’ because of
print statement. We know it very well
that if end character is not mentioned
in print statement then default end
character is ‘\n’
readlines()

syntax

list_name=file_handler_object.readlines()

Example

Output of the above code:-

You might also like