FILE HANDLING
INTRODUCTION
DATA FILES
OPENING ANDCLOSING FILES
READING ANDWRITING FILES
STANDARD INPUT, OUTPUTAND ERROR STREAMS
Introduction
FILE HANDLING is a mechanism by which we can read data of disk
files in python program or write back data from python program to
disk files.
So far in our python program the standard input in coming from
keyboard an output is going to monitor i.e. no where data is stored
permanent and entered data is present as long as program is running
BUT file handling allows us to store data entered through python
program permanently in disk file and later on we can read back the data
DATA FILES
It contains data pertaining to a specific application, for later
use. The data files can be stored in two ways –
Text File
Binary File
Text F i l e
Text file stores information in ASCII OR UNICODE character. In
text file everything will be stored as a character for example if
data is “computer” then it will take 8 bytes and if the data is
floating value like 11237.9876 it will take 10 bytes.
In text file each line is terminated by special character called
EOL. In text file some translation takes place when this EOL
character is read or written. In python EOL is ‘\n’ or ‘\r’ or
combination of both
Binary f i l e s
It stores the information in the same format as in the memory
i.e. data is stored according to its data type so no translation
occurs.
In binary file there is no delimiter for a new line
Binary files are faster and easier for a program to read and write
than text files.
Data in binary files cannot be directly read, it can be read only
through python program for the same.
Steps i n Data F i l e Handling
1. OPENING FILE
We should first open the file for read or write by specifying the name of file and
mode.
2. PERFORMING READ/WRITE
Once the file is opened now we can either read or write for which file is opened
using various functions available
3. CLOSING FILE
After performing operation we must close the file and release the file for other
application to use it,
Opening F i l e
File can be opened for either – read, write, append.
SYNTAX:
file_object = open(filename)
Or
file_object = open(filename,mode)
** default mode is “read”
myfile =open(“[Link]”)
here disk file “[Link]” is loaded in memory and its reference is linked to
“myfile” object, now python program will access “[Link]” through “myfile”object.
here “[Link]” is present in the same folder where .py file is stored otherwise if
disk file to work is in another folder we have to give full path.
myfile = open(“[Link]”,”r”)
here “r” is for read (although it is by default, other
options are “w” for write, “a” for append)
myfile = open(“d:\\mydata\\[Link]”,”r”)
here we are accessing “[Link]” file stored in
separate location i.e. d:\mydata [Link] the time of giving path of file we must use
double backslash(\\) in place of single backslash because in python single slash is used
for escape character and it may cause problem like if the folder name is “nitin” and we
provide path as d:\nitin\[Link] then in \nitin “\n” will become escape character for
new line, SO ALWAYS USE DOUBLE BACKSLASH IN PATH
myfile = open(“d:\\mydata\\[Link]”,”r”)
another solution of double backslash is using “r” before
the path making the string as raw string i.e. no special meaning
attached to any character as:
myfile = open(r“d:\mydata\[Link]”,”r”)
Fi l e Handle
myfile = open(r“d:\mydata\[Link]”,”r”)
In the above example “myfile” is the file object or file handle or
file pointer holding the reference of disk file. In python we will
access and manipulate the disk file through this file handle only.
F i l e Access Mode
Text Binary File Description Notes
File Mode
Mode
‘r’ ‘rb’ Read only File must exists, otherwise Python raises
I/O errors
‘w’ ‘wb’ Write only If file not exists, file is created
If file exists, python will truncate existing
data and overwrite the file.
‘a’ ‘ab’ Append File is in write mode only, new data will be
added to the end of existing data i.e. no
overwriting. If file not exists it is created
‘r+’ ‘r+b’ or ‘rb+’ Read and write File must exists otherwise error is raised Both
reading and writing can take place
w+ ‘w+b’ or ‘wb+’ Write and read File is created if not exists, if exists data will be
truncated, both read and write allowed
‘a+’ ‘a+b’ or ‘ab+’ Write and read Same as above but previous content will be
retained and both read and write.
Closing f i l e
As reference of disk file is stored in file handle so to close we must
call the close() function through the file handle and release the
file.
[Link]()
Note: open function is built-in function used standalone while close() must
be called through file handle
Reading from F i l e
To read from file python provide many functions like :
[Link]([n]) : reads and return n bytes, if n is not specified
it reads entire file.
[Link]([n]) : reads a line of input. If n is specified
reads at most n bytes. Read bytes in the form of string ending with
line character or blank string if no more bytes are left for reading.
[Link](): reads all lines and returns them in a list
Example-1: read()
SAMPLE FILE
Example-2: read()
SAMPLE FILE
Example-3: r e a d l i n e ( )
SAMPLE FILE
Example-3: read l i n e( )
SAMPLE FILE
Example-4: reading line by line()
SAMPLE FILE
Example-5: reading line by lineusing for loop
SAMPLE FILE
Example-6: Calculating size of f i l e
SAMPLE FILE
Exampl e- 7 : r eadl i n e s ( )
SAMPLE FILE
Writing onto f i l e s
After read operation, let us take an example of how to write data
in disk files. Python provides functions:
write ()
writelines()
The above functions are called by the file handle to write
desired content.
Name Syntax Description
write() [Link](str1) Writes string str1 to file
referenced by filehandle
Writelines() [Link](L) Writes all string in List L as lines to
file referenced by filehandle.
Example-1: wr i t e( ) usi ng “w” mode
Example-1: wr i t e( ) usi ng “w” mode
Now we can observe that while writing data to file using “w” mode the previous
content of existing file will be overwritten and new content will be saved.
If we want to add new data without overwriting the previous content then we
should write using “a” mode i.e. append mode.
Example-2: wr i t e( ) usi ng “a” mode
New content is
added after previous
content
Example-3: using writelines()
Exampl e- 4: Wr i t i ng St r i ng as a r ecor d to f i l e
Exampl e- 4: To copy t he cont ent of
one f i l e to another f i l e
f l u s h ( ) function
When we write any data to file, python hold everything in buffer
(temporary memory) and pushes it onto actual file later. If you
want to force Python to write the content of buffer onto
storage, you can use flush() function.
Python automatically flushes the files when closing them i.e. it
will be implicitly called by the close(), BUT if you want to flush
before closing any file you can use flush()
Exampl e: worki ng of f l ush( ) Nothing is in
the file
Without flush() [Link]
When you run the above code, program will
stopped at “Press any key”, for time being
don’t press any key and go to folder where
file “[Link]” is created an open it to see
what is in the file till now
NOW PRESS ANY KEY….
Now content is stored,
because of close() function
contents are flushed and
pushed in file
Exampl e: worki ng of f l ush( ) All contents
before flush()
With flush() are present
in file
When you run the above code, program will
stopped at “Press any key”, for time being
don’t press any key and go to folder where
file “[Link]” is created an open it to see
what is in the file till now
NOW PRESS ANY KEY….
Rest of the content is
written because of close(),
contents are flushed and
pushed in file.
Rem ovi ng whi t espaces af t er readi ng
from f i l e
read() and readline() reads data from file and return it in the
form of string and readlines() returns data in the form of
list.
All these read function also read leading and trailing
whitespaces, new line characters. If you want to remove
these characters you can use functions
strip() : removes the given character from both ends.
lstrip(): removes given character from left end
rstrip(): removes given character from right end
Example: s t r i p ( ) , l s t r i p ( ) , r s t r i p ( )
Fi l e Pointer
Every file maintains a file pointer which tells the current
position in the file where reading and writing operation will
take.
When we perform any read/write operation two things
happens:
The operation at the current position of file pointer
File pointer advances by the specified number of bytes.
Example
myfile = open(“[Link]”,”r”)
File pointer will be by default at first position i.e. first character
ch = [Link](1)
ch will store first character i.e. first character is consumed, and file pointer will
move to next character
Fi l e Modes and Openi ng posi t i on of f i l e
pointer
FILE MODE OPENING POSITION
r, r+, rb, rb+, r+b Beginning of file
w, w+, wb, wb+, w+b Beginning of file (overwrites the file if
file already exists
a, ab, a+, ab+, a+b At the end of file if file exists otherwise
creates a new file
Standard INPUT, OUTPUT and ERROR STREAM
Standard Input : Keyboard
Standard Output : Monitor
Standard error : Monitor
Standard Input devices(stdin)reads from keyboard
Standard output devices(stdout) display output on monitor
Standard error devices(stderr) sameas stdout but normally for
errors only.
Standard INPUT, OUTPUT and ERROR STREAM
The standard devices are implemented as files called
standard streams in Python and we can use them by
using sys module.
After importing sys module we can use standard
streams stdin, stdout, stderr
“with” statement
Python’s “with” statement for file handling is very handy when you
have two related operations which you would like to execute as a pair,
with a block of code in between:
with open(filename[, mode]) as filehandle:
file_manipulation_statement
The advantage of “with” is it will automatically close the file after
nested block of code. It guarantees to close the file how nested
block exits even if any run time error occurs
Example
Binary f i l e operations
If we want to write a structure such as list or dictionary to a
file and read it subsequently we need to use the Python
module pickle. Pickling is the process of converting structure
to a byte stream before writing to a file and while reading the
content of file a reverse process called Unpickling is used to
convert the byte stream back to the original format.
Steps to perform binary f i l e operations
First we need to import the module called pickle.
This module provides 2 main functions:
dump() : to write the object in file which is loaded in binary mode
Syntax : dump(object_to_write, filehandle)
load() : dumped data can be read from file using load() i.e. it is used to read
object from pickle file.
Syntax: object = load(filehandle)
Example: dump()
See the content is some kind
of encrypted format, and it is
not in complete readable
form
Example: load()
Absolute Vs Relative PATH
To understand PATH we must be familiar with the terms:
DRIVE, FOLDER/DIRECTORY, FILES.
Our hard disk is logically divided into many parts
called DRIVES like CDRIVE, D DRIVE etc.
Absolute Vs Relative PATH
The drive is the main container in which we put everything to
store.
The naming format is : DRIVE_LETTER:
For e.g. C: , D:
Drive is also known as ROOT DIRECTORY.
Drive contains Folder andFiles.
Folder contains sub-folders or files
Files are the actual data container.
Absolute Vs Relative PATH
DRIVE
FOLDER
DRIVE/FOLDER/FILE HIERARCHY
C:\
E
SALES IT HR PROD
FOLDE R FOLDE R FOLDE R FOLDE R
2018 2019 [Link] NOIDA DELHI
FOLDE R FOLDE R FOLDE R FOLDE R FOLDE R
[Link] [Link] SEC_8.XLS SEC_12.PPT
FILE FILE FILE FILE
Absolute Path
Absolute path is the full address of any file or folder from
the Drive i.e. from ROOT FOLDER. It is like:
Drive_Name:\Folder\Folder…\filename
For e.g. the Absolute path of file
[Link] will be
C:\SALES\2018\[Link]
Absolute path of SEC_12.PPT is
C:\PROD\NOIDA\Sec_12.ppt
Relative Path
Relative Path is the location of file/folder from the current folder.
To use Relative path special symbols are:
Single Dot ( . ) : single dot ( . ) refers to current folder.
Double Dot ( .. ) : double dot ( .. ) refers to parent folder
Backslash ( \ ) : first backslash before (.) and double dot( .. ) refers to
ROOT folder.
Relative addressing
Current working directory
C:\
DRIVE
SALES IT HR PROD
FOLDER FOLDER FOLDER FOLDER
2018 2019 [Link] NOIDA DELHI
FOLDER FOLDER FOLDER FOLDER FOLDER
[Link] [Link] SEC_8.XLS SEC_12.PPT
FILE FILE FILE FILE
SUPPOSE CURRENT WORKING DIRECTORY IS : SALES
WE WANT TO ACCESS [Link] FILE, THEN RELATIVEADDRESS WILL BE
.\2019\[Link]
Relative addressing
Current working
directory
C:\
DRIVE
SALES IT HR PROD
FOLDER FOLDER FOLDER FOLDER
2018 2019 [Link] NOIDA DELHI
FOLDER FOLDER FOLDER FOLDER FOLDER
[Link] [Link] SEC_8.XLS SEC_12.PPT
FILE FILE FILE FILE
SUPPOSE CURRENT WORKING DIRECTORY IS : DELHI
WE WANT TO ACCESS SEC_8.XLS FILE, THEN RELATIVEADDRESS WILL BE
..\NOIDA\SEC_8.XLS
Get t i ng name of cur r ent
wor ki ng directory
import os
pwd = [Link]()
print("Current Directory :",pwd)