Data File Handling - 1
Data File Handling - 1
A file (i.e. data file) is a named place on the disk where a sequence of related data is stored.
Basic operations performed on a data file are:
Naming a file
Opening a file
Reading data from the file
Writing data in the file
Closing a file
Using these basic operations, we can process file in many ways, such as
Creating a file
Traversing a file for displaying the data on screen
Appending data in file
Inserting data in file
Deleting data from file
Create a copy of file
Updating data in the file, etc.
Text
Binary
A text file is usually considered as sequence of lines. Line is a sequence of characters (ASCII),
stored on permanent storage media. Although default character coding in python is ASCII
but using constant u with string, supports Unicode as well. As we talk of lines in text file,
each line is terminated by a special character, known as End of Line (EOL). From strings we
know that \n is newline character. So at the lowest level, text file will be collection of bytes.
Text files are stored in human readable form and they can also be created using any text
editor
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. It is perfectly possible
to interpret a stream of bytes originally written as string, as numeric value. But we know
that will be incorrect interpretation of data and we are not going to get desired output after
the file processing activity. So in the case of binary file it is extremely important that we
interpret the correct data type while reading the file. Python provides special module(s) for
encoding and decoding of data for binary file.
To handle data files in python, we need to have a file object. Object can be created by using
open() function or file() function. To work on file, first thing we do is open it. This is done by
using built in function open(). Using this function a file object is created which is then used
for accessing various methods and functions available for file manipulation.
f=open(‘e:\\ops\\data.txt’,"r")
File object or
Path to file Mode
File-handle
r will open the text file for reading only and rb will do the same for binary format file. This
is also the default mode. The file pointer is placed at the beginning for reading purpose,
when we open a file in this mode.
w will open a text file for writing only and wb for binary format file. The file pointer is again
placed at the beginning. A non existing file will be created using this mode. Remember if we
open an already existing file (i.e. a file containing data) in this mode then the file will be
overwritten as the file pointer will be at the beginning for writing in it.
a mode allow us to append data in the text file and ab in binary file. Appending is writing
data at the end of the file. In this mode, file pointer is placed at the end in an existing file. It
can also be used for creating a file, by opening a non existing file using this mode.
r+ will open a text file and rb+ will open a binary file, for both reading and writing purpose.
The file pointer is placed at the beginning of the file when it is opened using r+ / rb+ mode.
w+ opens a file in text format and wb+ in binary format, for both writing and reading. File
pointer will be placed at the beginning for writing into it, so an existing file will be
overwritten. A new file can also be created using this mode.
a+ opens a text file and ab+ opens a binary file, for both appending and reading. File
pointer is placed at the end of the file, in an already existing file. Using this mode a non
existing file may be created.