FILE HANDLING AND EXCEPTIONS
Persistence
• Most of the programs we have seen so far are transient in the
sense that they run for a short time and produce some output,
but when they terminate, their data (variables) disappears. If you
run the program again, it starts with a clean slate.
• Some programs are persistent: they run for a long time (or all
the time); they keep at least some of their data in permanent
storage (a hard drive, for example); and if they shut down and
restart, they pick up where they left off.
• Examples of persistent programs are operating systems, which
run pretty much whenever a computer is on, and web servers,
which run all the time, waiting for requests from the network.
• One of the simple way for programs to maintain their data 2is
by storing data into files.
File
File:
A file is a chunk of logically related data or information which can be used by
computer programs.
A unique name and path is used by human users/programs to access a file for
reading and modification purposes.
Usually, a file resides on a durable storage. Durable means that the data is
persistent, i.e. it can be used by other programs after the program which has
created or manipulated it has terminated.
File Management:
It is the manipulation of data in a file/documents on a computer. It
includes Creating new file
Opening a file Writing to a file
Reading from a file Closing a
file Deleting a file 3
File Types
Text files
Text files are structured as a sequence of lines, where each line
includes a sequence of characters. Each line is terminated with a
special character, called the EOL(End of Line) character. It ends
the current line and tells the interpreter a new one has begun.
Binary Files
A binary file is any type of file that is not a text file. Because of
their nature, binary files can only be processed by an application
that know or understand the file’s structure. In other words, they
must be applications that can read and interpret binary.
4
Opening and Closing a File
• Files have to be opened before a program can read/write data from/to it
• The syntax to open a file is
file1 = open( “File_name.txt”, “Access_mode” )
• This function creates a file1 object referring to file and it can be utilized to
call other support methods associated with it.
• File_name − The file_name is a valid identifier that represents the name of
the file that you want to access.
• Access_mode − The access_mode represents the mode in which the file has
to be opened, i.e., read, write, append, etc. It is an optional parameter and
the default access mode is read (r).
• The file subdirectory path may have to be included in the file name
file1 = open( “c:\Users\Desktop\PythonPrograms\File_name.txt”, “r” )
• Files also have to be closed when a program is finished with it
5
• The syntax to close a file is
file1.close()
WHAT IS A HANDLE?
•
handle = open(filename, mode)
•
returns a handle use to manipulate the file
>>> fhand = open('mbox.txt')
>>> print(fhand)
<open file 'mbox.txt', mode 'r' at 0x1005088b0>
Access Modes: read mode
• r —- Opens a file for reading only. The file pointer
is placed at the beginning of the file. This is the
default mode.
• rb —- Opens a file for reading only in binary format.
The file pointer is placed at the beginning of the file.
This is the default mode.
• r+ —- Opens a file for both reading and writing.
The file pointer placed at the beginning of the file.
• rb+ —- Opens a file for both reading and writing in
binary format. The file pointer is placed at the
beginning of the file.
7
Access Modes: Write Mode
• w —Opens a file for writing only. Overwrites the
file if the file exists. If the file does not exist, creates
a new file for writing.
• wb — Opens a file for writing only in binary format.
Overwrites the file if the file exists. If the file does not
exist, creates a new file for writing.
• w+ —- Opens a file for both writing and reading.
Overwrites the existing file if file exists. If file does
not exist, creates a new file for reading and writing.
• wb+ —- Opens a file for both writing and reading in
binary format. Overwrites the existing file if the file
exists. If the file does not exist, creates a new file for
7
reading and writing.
Access Modes: Append Mode
• a —- Opens a file for appending. The file pointer is at the
end of the file if the file exists. That is, the file is in the
append mode. If the file does not exist, it creates a new
file for writing.
• ab —- Opens a file for appending in binary format. The file
pointer is at the end of the file if the file exists. That is, the
file is in the append mode. If the file does not exist, it creates
a new file for writing.
• a+ —- Opens a file for both appending and reading. The file
pointer is at the end of the file if the file exists. The file
opens in the append mode. If the file does not exist, it creates
a new file for reading and writing.
• ab+ — Opens a file for both appending and reading in binary
format. The file pointer is at the end of the file if the file
exists. The file opens in the append mode. If the file does not 7
exist, it creates a new file for reading and writing.
Attributes of File Object
The syntax to open a file is
file = open( “File_name.txt”, “Access_mode” )
The file object/handle can be used to get various
information related to opened file.
file.name — It returns name of the file.
file.mode — It returns access mode with which file was
opened.
file.closed — It returns true if file is closed, false
otherwise.
10
Writing To a File
To write a single string to a text file use the write() method. The syntax for write()
method is
file_Object.write( “str” )
• The argument of write has to be a string, so if we want to put other values in a file,
we have to convert them to strings. The easiest way to use str(value).
• The write() method writes any string to an open file. The write() method does not
add a newline character ('\n') to the end of the string.
Example:
file1.write(“This is a test…\n”)
file1.write(“This is only a test\n” )
Example:
file1 = open('F15.txt', 'w+')
for i in range(3):
file1.write('This is the first file\n')
file1.close() 11
Writing To a File
● The writelines() method is used to write multiple lines to a opened file i.e. It write
list of multiple strings to the file.
● The newline characters must be embedded in each string for proper formatting (as
needed)
● Example
file1.writelines( any_list )
● Example
file1 = open('F16.txt', 'w+')
file1.writelines(['This is the first line\n', 'This is the second line\n', 'This is the third
line'])
file1.close()
12
Writing To a File Using Format Operator
● Example
file1 = open('F16.txt', 'w+')
file1.write(‘The price of book is %f and it consists of %d chapters’%(890.5, 67))
file1.close()
13
Reading From a File
Ø There are several functions and methods for reading data from a file.
Ø The syntax of read() function is
file_Object.read( “count” )
Ø The read() method allows a program to read a specified number of characters
from a file and returns them as a string.
Ø The passed parameter is the number of bytes to be read from the opened file.
This method starts reading from the beginning/current position of the file and if
count is missing, then it tries to read as much as possible, maybe until the end of
file.
Ø Python remembers where the file last read data by using a “file pointer” or
“bookmark”.
Example
file1 = open( “test.txt”, “r” )
print(file1.read(5))
print(file1.read(4)) 14
file1.close()
Reading From a File
• All files have a “end of file” indicator (EOF) that signals to your
program that there is no more data to read in a file
• Trying to read past the EOF will return an empty string
Example
file1 = open('F1.txt', 'w+')
for i in range(4):
file1.write('This is the first file\n')
file1.close()
file1 = open('F1.txt', 'r')
print(file1.read())
print('Second read')
print(file1.read())
file1.close() 15
Reading Characters From a File
Text files are often line oriented and your program may have to read and process
one line at a time
The method readline() is used to read characters from the current line only
The syntax of read() function is
file_Object.readline( [count] )
The readline() method allows a program to read a specified number of
characters from a line and returns them as a string.
Example
string1 = file1.readline()
string1 = file1.readline(5)
String1 = file1.readline(200)
16
Reading all lines into a list
● Another way to work with lines from a file is to read each line (string) into a list.
● The method readlines() is used to read all the lines from a text file and store
them into a list of lines (strings)
● Example
file1 = open('F14.txt', 'w+')
for i in range(3):
file1.write('This is the first file\n')
file1.close()
file1 = open('F14.txt', 'r')
print( file1.readlines() )
file1.close()
• The output will be
['This is the first file\n', 'This is the first file\n', 'This is the first file\n']
17
Looping through a File
• Text files are a type of sequence delimited by lines
• Python programs can also read and process lines from text files
by using iteration
Example
file1 = open('F1.txt', 'w+')
for i in range(3):
file1.write('This is the first file\n')
file1.close()
file1 = open('F1.txt', 'r')
for line in file1:
print(line)
file1.close()
18
Read the number of Lines
file1 = open('F12.txt', 'w+')
for i in range(200):
file1.write('This is the first file\n')
file1.close()
file1 = open('F12.txt', 'r')
Lines = len(file1.readlines())
print('The total number of lines', Lines)
file1.close()
19
Read number of words
file1 = open('F1.txt', 'w+')
for i in range(20):
file1.write('This is the first file\n')
file1.close()
file1 = open('F1.txt', 'r')
Words = 0
for Line in file1:
NewStr = Line
WordsinLine = len(NewStr.split())
Words = Words + WordsinLine
print('The total number of words', Words)
file1.close() 20
Read number of Characters
file1 = open('F12.txt', 'w+')
for i in range(20):
file1.write('This is the first file\n')
file1.close()
file1 = open('F12.txt', 'r')
Characters = 0
for Line in file1:
print(Line)
Characters = Characters + len(Line)
print('The total number of characters', Characters)
file1.close()
19
Read from User and Write to File
file1 = open('F45.txt', 'w')
for i in range(5):
string1 = input('Enter the line')
string1 = string1 + '\n'
file1.write(string1)
file1.close()
file1 = open('F45.txt', 'r')
Characters = 0
Words =0
for Line in file1:
Characters = Characters + len(Line)
Words = Words + len(Line.split())
print('The total number of words and characters', Words, Characters)
file1.close()
22
Frequency of each Characters
file1 = open('F45.txt', 'w')
for i in range(5):
string1 = input('Enter the line')
string1 = string1 + '\n'
file1.write(string1)
file1.close()
file1 = open('F45.txt', 'r')
count = dict()
for Line in file1:
List = Line.split()
for words in List:
for letter in words:
count[letter] = count.get(letter, 0) +1
19
print('The frequency of each character', count)
file1.close()
Program to read itself to compute number of words
#File name program.py
file1 = open('program.py', 'r')
Words = 0
for Line in file1:
print(Line)
NewStr = Line
WordsinLine = len(NewStr.split())
Words = Words + WordsinLine
print('The total number of words', Words)
file1.close()
24
Program to read itself to compute frequency of each character
including space
#File name program.py
file1 = open('program.py', 'r')
Dict1 = dict()
words =0
for Line in file1:
print(Line)
Line1 = Line
words = words + len(Line1.split())
for letter in Line:
Dict1[letter] = Dict1.get(letter, 0) + 1
print('The frequency of each characters', Dict1)
print('Total number of words', words) 25
file1.close()
Copy File
Open a file (FirstFile)
Create one new file (SecondFile)
Copy content of FirstFile to the SecondFile.
26
Combine each line from first file with the corresponding line in
second file
file1 = open('F1.txt', 'w')
for i in range(3):
string1 = input('Enter a line for first file')
string1 = string1 + '\n'
file1.write(string1)
file1.close()
file2 = open('F2.txt', 'w')
for i in range(3):
string1 = input('Enter a line for second file')
string1 = string1 + '\n'
file2.write(string1)
file2.close()
file1 = open('F1.txt', 'r’);List1 = file1.readlines()
file2 = open('F2.txt', 'r’);List2 = file2.readlines()
file3 = open('F3.txt', 'w')
for i in range(3):
Line1 = List1[i]; Line2 = List2[i]
Line3 = Line1[:len(Line1)-1] + ' ' + Line2[:len(Line2)-1] +'\n'
file3.write(Line3)
27
file1.close();file2.close();file3.close()
file3 = open('F3.txt', 'r’);print('The content of file 3 is\n ', file3.read()); file3.close()
File Positions
tell(): This method specifies the current position within the file i.e. the next read or write will occur at that
many bytes from the beginning of the file.
seek(offset[, from]): This method changes the current file position. The offset argument indicates the
number of bytes to be moved. The from argument specifies the reference position from where the bytes are
to be moved.
The value 0 of from represents the beginning of the file
The value 1 of from represents the current position in the file
The value 2 of from represents the end of the file as the reference position.
Example
file1 = open('F16.txt', 'r+')
print( file1.read(10))
print( 'The current position is', file1.tell(), 'bytes')
file1.seek(0, 0)
print( 'The current position is', file1.tell(), 'bytes')
file1.close()
28
os Module
Python provides os module which consists of various methods to perform file-processing
operations, such as renaming and deleting files.
rename() — This method renames the existing file. It takes two arguments, the current
filename and the new file. Example: os.rename(Old_file, New_file)
remove() — This method remove the existing file. It take one argument as a file name to be removed.
Example: os.remove(file1)
29
File Methods
dir(file1)
['_CHUNK_SIZE', '__class__', '__del__', '__delattr__', '__dict__',
'__dir__', '__doc__', '__enter__', '__eq__', '__exit__', '__format__',
'__ge__', '__getattribute__', '__getstate__', '__gt__', '__hash__',
'__init__', '__init_subclass__', '__iter__', '__le__', '__lt__', '__ne__',
'__new__', '__next__', '__reduce__', '__reduce_ex__', '__repr__',
'__setattr__', '__sizeof__', '__str__', '__subclasshook__',
'_checkClosed', '_checkReadable', '_checkSeekable',a
'_checkWritable', '_finalizing', 'buffer', 'close', 'closed', 'detach',
'encoding', 'errors', 'fileno', 'flush', 'isatty', 'line_buffering',
'mode', 'name', 'newlines', 'read', 'readable', 'readline',
'readlines', 'reconfigure', 'seek', 'seekable', 'tell', 'truncate',
'writable', 'write', 'write_through', 'writelines’]
30
Programs
Write a Python program to read first n lines of a file.
Write a Python program to append text to a file and display the text.
Write a Python program to read last n lines of a file.
Write a Python program to read a file line by line and store it into a list.
Write a Python program to read a file line by line store it into a variable.
Write a python program to find the longest words.
Write a Python program to count the number of lines in a text file.
Write a Python program to count the frequency of words in a file.
Write a Python program to count the frequency of characters in a file.
Write a Python program to copy the contents of a file to another file.
Write a Python program to combine each line from first file with the
corresponding line in second file.
31
PYTHON EXCEPTIONS HANDLING
Python provides very important feature to handle any unexpected error and to
add debugging capabilities in them:
● Exception Handling
What is an exception?
Ø An exception is an event, which occurs during the execution of a program, that
disrupts the normal flow of the program's instructions.
Ø In general, when a Python script encounters a situation that it can't handle, it raises
an exception. An exception is a Python object that represents an error.
Ø When a Python script raises an exception, it must either handle the exception
immediately otherwise it would terminate and come out.
Ø When an error occurs, Python will normally stops and generates an error message.
Example
A = list(range(10)) A = list(range(20))
sum =0; sum =0;
for i in range(21): for i in range(21):
print('The element no %d is’%A[i]) try:
sum = sum + A[i] print('The element no %d is'%A[i])
print('The sum of elements are', sum) sum = sum + A[i]
except:
#This program will terminate without print('The list index is out of range')
printing sum.
print('The sum of elements are', sum)
Handling an exception:
○ If you have some suspicious code that may raise an exception, you can defend
your program by placing the suspicious code in a try: block. After the try:
block, include an except: statement, followed by a block of code which
handles the problem as elegantly as possible.
Syntax:
try:
You do your operations here;
......................
except Exception I:
If there is ExceptionI, then execute this block.
except Exception II:
If there is ExceptionII, then execute this block.
......................
else:
If there is no exception then execute this block.
Here are few important points above the above mentioned syntax:
○ A single try statement can have multiple except statements. This is useful when the
try block contains statements that may throw different types of exceptions.
○ You can also provide a generic except clause, which handles any exception.
try:
You do your operations here;
......................
except:
If there is ExceptionII, then execute this block.
......................
else:
If there is no exception then execute this block.
○ After the except clause(s), you can include an else-clause. The code in the else-block
executes if the code in the try: block does not raise an exception.
Example:
try:
fh = open("F57.txt", "r")
fh.write(”my name is Michael”)
except IOError:
print("Error: can\'t find file or read data")
else:
print("Written content in the file successfully")
fh.close()
The except clause with no exceptions:
You can also use the except statement with no exceptions defined as
follows:
try:
You do your operations here;
......................
except:
If there is any exception, then execute this
block. ......................
else:
If there is no exception then execute this block.
This kind of a try-except statement catches all the exceptions that
occur. Using this kind of try-except statement is not considered a good
programming practice, though, because it catches all exceptions but
does not make the programmer identify the root cause of the problem
that may occur.
The except clause with multiple exceptions:
You can also use the same except statement to handle multiple exceptions as
follows:
try:
You do your operations here;
......................
except(Exception1[, Exception2[,...ExceptionN]]]):
If there is any exception from the given exception
list, then execute this block
.......................
else:
If there is no exception then execute this block.
Standard Exceptions:
Check Standard Exceptions
The try-finally clause:
You can use a finally: block along with a try: block. The finally block is a
place to put any code that must execute, whether the try-block raised an
exception or not. The syntax of the try-finally statement is this:
try:
You do your operations here;
......................
Due to any exception, this may be skipped.
finally:
This would always be executed.
......................
Note that you can provide except clause(s), or a finally clause, but not both.
You can not use else clause as well along with a finally clause.
Example:
try:
fh = open("testfile", "w")
fh.write("This is my test file for exception
handling!!")
finally:
print(“Error: can\'t find file or read data”)
If you do not have permission to open the file in writing mode then this
will produce following result:
Error: can't find file or read data
Raising an exceptions:
You can raise exceptions in several ways by using the raise statement. The
general syntax for the raise statement.
Syntax:
raise [Exception [, args [, traceback]]]
○ Here Exception is the type of exception (for example, NameError) and
argument is a value for the exception argument. The argument is optional; if
not supplied, the exception argument is None.
○ The final argument, traceback, is also optional (and rarely used in practice),
and, if present, is the traceback object used for the exception
Example:
def functionName( level ):
if level < 1:
raise "Invalid level!", level
# The code below to this would not be executed
# if we raise the exception
Note: In order to catch an exception, an "except" clause must refer to
the same exception thrown either class object or simple string. For
example to capture above exception we must write our except clause
as follows:
try:
Business Logic here...
except "Invalid level!":
Exception handling here...
else:
Rest of the code here...
User-Defined Exceptions:
○ Python also allows you to create your own exceptions by deriving classes
from the standard built-in exceptions.
○ Here is an example related to RuntimeError. Here a class is created that is
subclassed from RuntimeError. This is useful when you need to display more
specific information when an exception is caught.
○ In the try block, the user-defined exception is raised and caught in the except
block. The variable e is used to create an instance of the class Networkerror.
class Networkerror(RuntimeError):
def init (self, arg):
self.args = arg
○ So once you defined above class, you can raise your exception as follows:
try:
raise Networkerror("Bad hostname")
except Networkerror,e:
print e.args
Thank You