File Handling
File Handling
INTRODUCTION
DATA FILES
OPENINGAND CLOSING FILES
READING ANDWRITING FILES
STANDARD INPUT, OUTPUTAND ERROR STREAMS
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 File
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 File
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 File
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”)
File Handle
myfile = open(r“d:\mydata\poem.txt”,”r”)
myfile.close()
SAMPLE FILE
Example-8 & 9: counting size foorfmofreilupedsateisnvisitb:ywwtwe.spythaonnd4csnpi o.c.om
of lines
SAMPLE FILE
Writing onto files
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() Filehandle.write(str1) Writes string str1 to file referenced
by filehandle
Writelines() Filehandle.writelines(L) Writes all string in List L as lines to
file referenced by filehandle.
Example-1: write() using “w” mode
Example-1: write() using “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: write() using “a” mode
New content is
added after previous
content
Example-3: using writelines()
Example-4: Writing String as a record
to file
Example-4: To copy the content of one
file to another file
flush() function
ch = myfile.read(1)
ch will store first character i.e. first character is consumed, and file pointer will
move to next character
File Modes and Opening position
of file 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
Example: dump()
C:\
DRIV E
SALES IT HR PROD
FOLDE R FOLDE R FOLDE R FOLDE R
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
REVENUE.TXT will be
C:\SALES\2018\REVENUE.TXT
Absolute path of SEC_12.PPT is
C:\PROD\NOIDA\Sec_12.ppt
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