Ch5 File Handling
Ch5 File Handling
Files are used to store huge collection of data and records permanently.
Many applications require large amount of data. In such situation, we need to use
some devices such as hard disk, compact disc etc., to store the data.
Types of file:
File Manipulation
One of the key features of most object oriented programming languages is
their ability to manipulate files. This involves creating files, reading from them,
opening them, writing to them, and appending text (or data) to them.
Basic Operations on a Text File
• Open a text file: Files in Python can be opened with a built-in open()
function. The open() function creates a file object which would be utilized to call
other methods associated with it.
Syntax:
file_object=open(filename[ access_mode],[ buffering])
• filename: The file name argument is a string value that contains the
name of the file that you want to access.
Buffering:
If the buffering value is set to 0, no buffering will take place. If the buffering
value is 1, line buffering will be performed while accessing a file. If you specify
the buffering value as an integer greater than 1, then buffering action will be
performed with the indicated buffer size. If negative, the buffer size is the system
default (default behaviour).
Close a text file: After opening and performing the reading, writing operations, it is
important to close the file. This is done using .close() method.
Syntax: file.close()
Python's sys module provides us with file-like objects that represent stdin, stdout, and
stderr. stdin , stdout , and stdderr variables contain stream objects corresponding to
standard I/O streams.
Standard input: This is the file-handle that a user program reads to get information
from the user. We give input to the standard input (stdin).
Standard output: The user program writes normal information to this file-handle.
The output is returned via the Standard output (stdout).
Standard error: The user program writes error information to this file-handle.
Errors are returned via the Standard error (stderr). After importing sys Module we
can use these Standard Streams in the same way you use other files
The absolute path is the full path to some place on your computer. It describes how to
access a given file or directory starting from the root of the file system.
The relative path is the path to some file with respect to your current working directory
(PWD).
For example, for the file text1.txt which is store in C directory ->folder -> subfolder1 ->
subfolder2.
Absolute path: C:/folder/ subfolder1 / subfolder2/text1.txt
If PWD is C:/folder/ subfolder1 /, then the relative path to text1.txt would be:
subfolder2/text1.txt
Binary File
Binary files store data in the binary format (0’s and 1’s) which is understandable by the
machine. So when we open the binary file in our machine, it decodes the data and
displays in a humanreadable format.
Opening and closing of binary file is same as text file opening and closing. While
opening any binary file we have to specify ‘b’ in file opening mode. Hence the "rb" mode
opens the file in binary format for reading, while the "wb" mode opens the file in binary
format for writing.
Serialization:
Serialization 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.
Pickle module provides us with the ability to serialize and de-serialize objects, i.e., to
convert objects into bitstreams which can be stored into files and later be used to
reconstruct the original objects. Serialization and De-serialization process is also called
pickling and un-pickling respectively.
Where data_object is the object that has to be dumped to the file with the file handle
named file_ object.
Here, the pickled Python object is loaded from the file having a file handle named
file_object and is stored in a new file handle called store_object
tell( ) function: The tell( ) function returns the current position of the file pointer in the
file.
Syntax: <fileobject>.tell( )
seek( ) function: The seek ( ) function changes the position of the file pointer by placing
the file pointer at the specified position in the open file.
Syntax: .seek( offset[,mode])
CSV File Handling
Reading from a CSV file is done using the reader object. The CSV file is opened as a text
file with Python’s built in open() function, which returns a file object.
Example: import CSV with open("Employee.txt") as CSV_file:
we open file in ‘w’ writing mode using open() method which create newFile like object.
When we have a set of data that we would like to store in a CSV file use csv.writer()
function. To iterate the data over the rows(lines), use the csv.writerow() function.
e.g
import CSV
with open ('Employee_file.CSV', mode='w') as Employee_file:
Employee_writer = CSV.writer (Employee_file, delimiter = ',’)
Employee_writer.writerow(['Rahul', 'Manager', 'April']