Data File Handling-01
Data File Handling-01
A file (i.e. data file) is a named place on the disk where a sequence of related data is
stored. In python files are simply stream of data, so the structure of data is not stored in the
file, along with data.
File handling in Python enables us to create, update, read, and delete the files stored on the
file system through our python program. The following operations can be performed on a file.
In Python, File Handling consists of following three steps:
Page | 2
Mrinmoy Paul (PGT-CS & IP)
FILE ACCESS MODES
2. readline() Method
readline() will return a line read, as a string from the file. First call to function will return first
line, second call next line and so on. It's syntax is,
fileobject.readline()
Page | 3
Mrinmoy Paul (PGT-CS & IP)
readline() will return only one line from a file, but notes.txt file containing three lines of
text.
3. readlines() Method
readlines()can be used to read the entire content of the file. You need to be careful while
using it w.r.t. size of memory required before using the function. The method will return a list
of strings, each separated by \n. It's syntax is,
fileobject.readlines()
As it returns a list, which can then be used for manipulation.
4. read(size) Method
read() can be used to read specific size string from file. This function also returns a string read
from the file. Syntax of read() function is:
fileobject.read([size])
For Example:
f.read(1) will read single byte or character from a file.
Page | 4
Mrinmoy Paul (PGT-CS & IP)
WRITING INTO FILE
A Program writes into a text/binary file from hard disk.
WRITING METHODS
1. write() Method
2. writelines() Method
1. write () Method
For sending data in file, i.e. to create / write in the file, write() and writelines() methods can
be used. write() method takes a string ( as parameter ) and writes it in the file.
For storing data with end of line character, you will have to add \n character to end of
the string
*Use ‘a’ in open function to append or add the information to the file. ‘a+’ to add as well
as read the file.
If you execute the program n times, the file is opened in w mode meaning it deletes content
of file and writes fresh every time you run.
2. writelines() Method
For writing a string at a time, we use write() method, it can't be used for writing a list, tuple
etc. into a file. Sequence data type can be written using writelines() method in the file. It's not
that, we can't write a string using writelines() method.
It's syntax is:
fileobject.writelines(seq)
RANDOM ACCESS METHODS
All reading and writing functions discussed till now, work sequentially in the file. To access
the contents of file randomly –following methods are use.
1. seek method
2. tell method
Page | 5
Mrinmoy Paul (PGT-CS & IP)
1. seek method
seek() method can be used to position the file object/file pointer at particular place in the file.
It's syntax is :
fileobject.seek(offset [, from_what])
here offset is used to calculate the position of fileobject in the file in bytes. Offset is added
to from_what (reference point) to get the position. Following is the list of from_what values:
Value reference point
0 - beginning of the file
1 - current position of file
2 - end of file
default value of from_what is 0, i.e. beginning of the file.
f.seek(7) keeps file pointer at reads the file content from 8th position onwards to till EOF.
2. tell method
tell() method returns an integer giving the current position of object/file pointer in the file.
The integer returned specifies the number of bytes from the beginning of the file till the
current position of file object.
It's syntax is: fileobject.tell()
tell() method returns an integer and assigned to pos variable. It is the current position from
the beginning of file.
Page | 6
Mrinmoy Paul (PGT-CS & IP)