You are on page 1of 18

File Handling

Introduction
• A file is a collection of records. A record is a
group of related data items.
• A file is a collection of numbers, symbols and
text and can be considered a stream of
characters.
• To permanently store the data created in a
program, a user needs to save it in a File on a
disk or some other device.
Cont’d
Various operations carried out on a file are:
• Creating a file
• Opening a file
• Reading from a file
• Writing to a file
• Closing a file
Opening a file
• A file needs to open before we can perform
read and write operations on it. To open a file,
a user needs to first create a file.
• The syntax to open a file is:
file object=open(file_name, [access_mode],
[buffering])
• There are various access modes available
i) R- opens a file for reading
ii) W-opens a file for writing. If a file already
exits, the contents are erased.
iii) A- opens file for appending data from the
end of the file
iv) Wb- opens a file for writing binary data
v) Rb- opens a file for reading binary data
• The third parameter [buffering] is optional,
which controls the buffering of a file
• Buffering parameter = 1, line buffering
• Buffering parameter = 0, no buffering
• Buffering parameter > 1, buffering according
to indicated buffer size
• Example:-
F1=open(“demo.txt”, “r”)
Writing text to a file
• The open function creates a file object. It is an
instance of _io.TextIOWrapper class. It
contains methods for reading and writing
data.
• Various methods available are:-
i) str readline() – returns the next line of a file
as a string
ii) list readline() – returns a list containing all
the lines in a file
iii) str read([int number]) - returns a specified
number of characters from a file. If there is no
argument, entire content of the file is read.
iv) Write (str s) – writes strings to a file
v) close() – closes a file
• Once a file is opened, the write method is used to
write a string to file.
• When a file is opened for reading or writing, file
pointer is positioned in the file that moves
forward as we start reading or writing a file
Closing a file
• After the successful completion of reading or
writing, there is a need to properly close a file.
This is done with the help of close()
Example:-
obj1 = open(“demo.txt”, “w”)
obj1.close()
Writing numbers to a file
• The write() method expects a string as an
argument. Therefore if we want to write other
data types, there is a need to convert that
data type into strings before writing them to
an output file.
Reading text from a file
• Once a file is opened using open(), its content
is loaded into the memory. The pointer points
to the very first character of the file.
• To read the content of the file, we open the
file in ‘r’ read mode.
• Example:-
Obj1=open(“demo.txt”,”r”)
• There are two common approaches to read
the content the file:
i) read() – read all data from a file and return
as one complete string
ii) readlines() – read all data and return as a list
of strings
Appending data
• The append “a” mode of file is used to append
data to the end of an existing file.
• The old content of the file is not erased.
• The file pointer will be in the end of the file
seek() function
• Whenever a file is opened just for reading and
writing, file pointer is positioned in the
beginning of the file.
• Python provides the inbuilt function seek() for
moving the pointer explicitly to any position in
a file.
• It is used to set the file pointer to a specific
position in a file.
• The syntax is:-
File_object.seek(offset, whence)
• Offset indicates the number of bytes to be
moved from the current position of the
pointer.
• Whence indicates the point of reference from
where the bytes are to be moved from.
i) 0 = the position is relative to the start of the
file. It is the default setting
ii) 1 = position is relative to the current position
iii) 2 = position is relative to the end of the file
Binary Files
• The modes used to read and write the binary
files are “rb” that is used to read from a binary
file and “wb” to write into a binary file.
• Binary files don’t have text in them.
• It might have pictures, music or some other
kind of data.
• No new line is there in case of binary file.
Reading binary files
• Binary file format begins with a specific series
of bytes to identify the file type.
• The first one byte in a jpg image is always
b’\xff\xd8’, it indicates the type of file.
• \xff\xd9 indicates the end of a file.

You might also like