File Handling - Subhamoy Sir
File Handling - Subhamoy Sir
✓ INTRODUCTION
✓ DATA FILES
✓ OPENINGAND CLOSING FILES
✓ READINGANDWRITING FILES
✓ STANDARD INPUT, OUTPUTAND ERROR STREAMS
▪ 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
▪ 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 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 like 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
▪ 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.
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,
▪ File can be opened for either – read, write,
append.
SYNTAX:
file_object = open(filename)
Or
file_object = open(filename,mode)
myfile = open(“d:\\mydata\\poem.txt”,”r”)
here we are accessing “poem.txt” file stored in
separate location i.e. d:\mydata folder.
at 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\poem.txt then in \nitin “\n” will become escape
character for new line, SO ALWAYS USE DOUBLE
BACKSLASH IN PATH
myfile = open(“d:\\mydata\\poem.txt”,”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\poem.txt”,”r”)
myfile = open(r“d:\mydata\poem.txt”,”r”)
myfile.close()
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.
write() using “a” mode
New content is
added after previous
content
using writelines()
▪ 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()
Nothing is in
the file
temp.txt
Without flush()
ch = myfile.read(1)
ch will store first character i.e. first character is consumed, and file pointer will
move to next character
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 : Keyboard
▪ Standard Output : Monitor
▪ Standard error : Monitor
C:\
DRIV E
SALES IT HR PROD
FOLDE R FOLDE R FOLDE R FOLDE R
SALES IT HR PROD
FOLDER FOLDER FOLDER FOLDER
.\2019\SHEET.XLS
Current working
directory
C:\
DRIVE
SALES IT HR PROD
FOLDER FOLDER FOLDER FOLDER