005. FILE HANDLING
005. FILE HANDLING
✓ INTRODUCTION
✓ DATAFILES
✓ OPENING ANDCLOSING FILES
✓ READING ANDWRITING FILES
✓ STANDARD INPUT,OUTPUTAND ERRORSTREAMS
Introduction
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
myfile =open(“story.txt”)
here disk file “story.txt” is loaded in
memory and its reference is linked to “myfile”
object, now python program will access
“story.txt” through “myfile”object.
here “story.txt” 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.
Opening F i l e
myfile = open(“article.txt”,”r”)
here “r” is for read (although it is by default, other
options are “w” for write, “a” for append)
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
Opening F i l e
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”)
Fi l e Handle
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.
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
ch =myfile.read(1)
ch will store first character i.e. first character is consumed, and file pointer will
move to nextcharacter
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
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
Relative addressing
Current working
directory
C:\
DRIVE
SALES IT HR PROD
FOLDER FOLDER FOLDER FOLDER