0% found this document useful (0 votes)
33 views6 pages

Data File Handling-01

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)
33 views6 pages

Data File Handling-01

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/ 6

DATA FILE HANDLING

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:

➢ Open the file.


➢ Process file i.e perform read or write operation.
➢ Close the file.
Need for a data file:
• To Store data in organized manner • To store data permanently • To access data faster • To
Search data faster • To easily modify data later on
Using these basic operations, we can process file in many ways, such as:
1. CREATING A FILE
2. TRAVERSING A FILE FOR DISPLAYING THE DATA ON SCREEN
3. APPENDING DATA IN FILE
4. INSERTING DATA IN FILE
5. DELETING DATA FROM FILE
6. CREATE A COPY OF FILE
7. UPDATING DATA IN THE FILE etc
Python allow us to create and manage two types of file:
1. TEXT FILE
2. BINARY FILE

What is Text File?


A text file is usually considered as sequence of lines. Line is a sequence of characters
(ASCII or UNICODE), stored on permanent storage media. The default character coding in
python is ASCII each line is terminated by a special character, known as End of Line (EOL).
Some of the example of text files.e.g. .txt,.rtf,.csv etc.
What is Binary File?
A binary file contains arbitrary binary data i.e. numbers stored in the file, can be used
for numerical operation(s). So when we work on binary file, we have to interpret the raw bit
pattern(s) read from the file into correct type of data in our program. Python provides special
Page | 1
Mrinmoy Paul (PGT-CS & IP)
module(s) for encoding and decoding of data for binary file. The .exe files,mp3 file, image
files, word documents are some of the examples of binary files.

OPENING AND CLOSING FILES


To perform file operation, it must be opened first then after reading, writing, editing
operation can be performed. To create any new file then too it must be opened. On opening
of any file, a file relevant structure is created in memory as well as memory space is created
to store contents. Once we are done working with the file, we should close the file. Closing a
file releases valuable system resources.
To handle data files in python, we need to have a file object. Object can be created by
using open() function or file() function.
Syntax of open() function is:
file_object = open(filename [, access_mode] [,buffering])
open() requires three arguments to work,
First one ( filename ) is the name of the file on secondary storage media, which can be string
constant or a variable. The name can include the description of path, in case, the file does not
reside in the same folder / directory in which we are working.
The second parameter (access_mode) describes how file will be used throughout the
program. This is an optional parameter and the default access_mode is reading.
The third parameter (buffering) is for specifying how much is read from the file in one read.
While writing the content in the file, first it goes to buffer and once the buffer is full,
data is written to the file. Also when file is closed, any unsaved data is transferred to file.
flush() function is used to force transfer of data from buffer to file.
CLOSE FUNCTION
fileobject.close() will be used to close the file object, once we have finished working on it. The
method will free up all the system resources used by the file, this means that once file is
closed, we will not be able to use the file object any more.
For example: fobj.close()

Page | 2
Mrinmoy Paul (PGT-CS & IP)
FILE ACCESS MODES

 FILE READING METHODS


A Program reads a text/binary file from hard disk. File acts like an input to a python
program. Followings are the methods to read a data from the file.
1. read() Method
2. readline() Method
3. readlines() Method
1. read() Method
The read() method is used to read entire file. The syntax is:
fileobject.read()

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)

You might also like