[go: up one dir, main page]

0% found this document useful (0 votes)
49 views32 pages

23CS101T PSPP - Unit 5

The document discusses files, modules, and packages in Python. It covers opening, reading, and closing text and binary files. It also discusses file paths, opening files using with keyword, and file object attributes like name, mode, and closed. Modules and packages are also briefly introduced.

Uploaded by

p rajeshwari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views32 pages

23CS101T PSPP - Unit 5

The document discusses files, modules, and packages in Python. It covers opening, reading, and closing text and binary files. It also discusses file paths, opening files using with keyword, and file object attributes like name, mode, and closed. Modules and packages are also briefly introduced.

Uploaded by

p rajeshwari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

UNIT 5 - FILES, MODULES, PACKAGES

Files and Exceptions: Text files, Reading and Writing files, format operator;
command line arguments, errors and exceptions, handling exceptions, modules,
packages; Illustrative programs: word count, copy file, Voter’s age validation,
Marks range validation(0-100)

FILE
A file is a collection of data stored on a secondary storage device like hard disk. A file
is basically used because real-life applications involve large amounts of data and in
such situations the console oriented I/O operations pose two major problems:
 First, it becomes cumbersome and time consuming to handle huge amount of data
through terminals.
 Second, when doing I/O using terminal, the entire data is lost when either the program
is terminated or computer is turned off. Therefore, it becomes necessary to store data on
a permanent storage (the disks) and read whenever necessary, without destroying the
data.
File Path
Files are stored on a storage medium like the hard disk in such a way that they can be
easily retrieved as and when required.
Every file is identified by its path that begins from the root node or the root folder.
In Windows, C:\ (also known as C drive) is the root folder but you can also have a path
that starts from other drives like D:\, E:\, etc. The file path is also known as pathname.
A file path can be either relative or absolute.

Absolute Path : Absolute path always contains the root and the complete directory list
to specify the exact location the file.
Ex. C:\Students\Under Graduate\BTech_CS.docx
Relative Path : Relative path needs to be combined with another path in order to
access a file. It starts with respect to the current working directory and therefore lacks
the leading slashes.
Ex. Under Graduate\BTech_CS.docx is a relative path as only a part of thecomplete
path is specified.

Types of Files
Text File :
A text file is a stream of characters that can be sequentially processed by a computer in
forward direction. For this reason a text file is usually opened for only one kind of
operation (reading, writing, or appending) at any given time. Because text files can
process characters, they can only read or write data one character at a time.
Depending on the requirements of the operating system and on the operation that has to
be performed (read/write operation) on the file, the newline characters may be
converted to or from carriage-return/linefeed combinations. Besides this, other
character conversions may also be done to satisfy the storage requirements of the
operating system. However, these conversions occur transparently to process a text file.
In a text file, each line contains zero or more characters and ends with one or more
characters
Another important thing is that when a text file is used, there are actually two
representations of data- internal or external. For example, an integer value will be
represented as a number that occupies 2 or 4 bytes of memory internally but externally
the integer value will be represented as a string of characters representing its decimal or
hexadecimal value.
Binary File:
A binary file is a file which may contain any type of data, encoded in binary form for
computer storage and processing purposes. It includes files such as word processing
documents, PDFs, images, spreadsheets, videos, zip files and other executable
programs. Like a text file, a binary file is a collection of bytes. A binary file is also
referred to as a character stream with following two essential differences.
• A binary file does not require any special processing of the data and each byte of data
is transferred to or from the disk unprocessed.
• Python places no constructs on the file, and it may be read from, or written to, in any
manner the programmer wants.
While text files can be processed sequentially, binary files, on the other hand, can be
either processed sequentially or randomly depending on the needs of the application.

Opening files
Before reading from or writing to a file, it must first be opened using Python‘sbuilt-in
open() function.
open ( ) function creates a file object, which will be used to invoke methods
assosciated with it.
The syntax of open() is:
fileObj = open(file_name [, access_mode])
Here,
file_name is a string value that specifies name of the file that you want to access.

access_modeindicates the mode in which the file has to be opened, i.e., read, write,
append, etc.
open function() -Access Modes

Mode Purpose

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
placed at the beginning of the file.

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 the
file exists. If the 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
reading and writing.

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 pointeris
at the end of the file if the file exists. The file opens in the append mode. Ifthe
file does not exist, it creates a new file for reading and writing.

Program
file = open (“File1.txt”, “r”)
print (file)
Output
< open file „File1.txt‟, mode „r‟ at 0x02A850D0 >

Opening files using with keyword


It is good programming habit to use the with keyword when working with fileobjects.
This has the advantage that the file is properly closed after it is used even if an error
occurs during read or write operation or even when forgot to explicitly closethe file.
Program
with open(“file1.txt”, “r”) as file:
for line in file:
print(line)
print(“Let‟s check if file is closed:”, file.closed)
Output
Hello World
True

Closing files
close() Method
The close() method is used to close the file object.
While closing the file object the close() flushes any unwritten information. Once a file
object is closed, reading and from or writing into the file associated with the file object
cannot be further done.
Although, Python automatically closes a file when the reference object of a file is
reassigned to another file, but as a good programming habit to always explicitly use the
close() method to close a file.
The syntax of close() is
fileObj.close()

The close() method frees up any system resources such as file descriptors, file locks,
etc. that are associated with the file.
Moreover, there is an upper limit to the number of files a program can open. If that
limit is exceeded then the program may even crash or work in unexpected manner.
Lots of memory is wasted if many files are open unnecessarily and open files always
stand a chance of corruption and data loss.
Once the file is closed using the close() method, any attempt to use the file object will
result in an error.
File object Attributes
Once a file is successfully opened, a file object is returned.
Using this file object, different type of information related to that file is easily accessed
by reading specific attributes of the file.

Attribute Description

fileObject.closed Returns true if file is closed and false otherwise.

fileObject.mode Returns access mode with which file was opened.

fileObject.name Returns name of the file.

file.softspace Returns false if space explicitly required with print, true


otherwise.

Program:
file = open (“File1.txt”, “w”)
print (“Name of the file :”, file.name)
print (“File is closed:”, file.closed)
print (“File has been opened in”, file.mode,”mode”)
Output
Name of the file : File1.txt
File is closed: False
File has been opened in w mode
Reading files
1. The read method
The read() method is used to read a string from an already opened file.
The string can include, alphabets, numbers, characters or other symbols.
using 'r' or 'rb' mode and then start reading
data.
The syntax of read() method is
fileObject.read([size])
where size is an optional parameter which if passed to the read() method specifies
the number of bytes to be read from the opened file.

The read() method starts reading from the beginning of the file and if size is missing
or has a negative value then, it reads the entire contents of the file (i.e., till the end
of file).

Program
file = open (“File1.txt”, “r”)
print (file.read(10))
file.close ( )
Output
Hello All

2. readline method
The readline( ) method is used to read individual lines of a file. It reads a file
untilthe newline (\n) character is reached or end of file (EOF).
Syntax :
fileObject.readline([size])
Here size is the optional numeric argument and returns a quantity of data equal to
size.
Program
file = open (“File1.txt”, “r”)
print (file.readline())
print (file.readline())
print (file.readline())
file.close ( )
Output

3. readlines method
The readlines() method returns a list of remaining lines on the entire file. It returns
empty string when end of file (EOF) is reached.
Syntax :
fileObject.readlines([size])
Here size is the optional numeric argument and returns a quantity of data equal to
size.

Program
file = open (“File1.txt”, “r”)
print (file.readlines( ))
file.close ( )
Output
[„Chennai\n‟,„Tamilnadu\n‟ , „India\n‟]

4. Read a file line-by-line – USING FOR LOOP


 Read a file line-by-line includes iterating over a file object in a for loop
Program
file1 = open('myfile.txt', 'r‟)
count = 0
print("Using for loop")
for line in file1:
count += 1
print("Line{}: {}".format(count, line.strip()))
file1.close()

Output
Line1: Geeks
Line2: for

Line3: Geeks

Writing files
1. The write() Method
The write() method is used to write a string to an already opened file.
include numbers, special characters or other symbols.
('\n') to the
end of the string.
'w' or 'wb' mode and then start writing data
into it, where its existing contents would be overwritten.
The syntax of write() method is:
fileObject.write(string)
Program
file = open (“File1.txt”, “w”)
file.write(“ Hello All, hope everyone are enjoying with
Python”)
file.close ( )
print (“Data written into the file“)
Output
Data written into the file

2. writelines ( ) Method
The writelines() method is used to write a sequence of strings into the file.
The sequence can be any iterable object which produces a list of strings. It has no
return value.
The syntax of writelines ( ) method is:
fileObject.writelines (sequence)

Program
file = open (“File1.txt”, “w”)
lines=[“Hello world”, “Welcome”, “Enjoy learning
Python”]
file.writelines(lines)
file.close ( )
print (“List data written into the file“)
Output
List data written into the file
3. To append data and write in files
is to open the stored file again to write more data or append data to it
instead of overwriting.
using 'a' or 'ab' mode depending on whether it is a
text file or a binary file.
or combining a
large set of data into one file.
Program
file = open (“File1.txt”, “a”)
file.write(“ Python is an Interpreter based language”)
file.close ( )
print (“Data appended into the file“)
Output
Data appended into the file

Other File Methods


Python has several file methods that can handle files and some of which are as
below
1. flush ( ) method :
This method forces out unsaved data that exists in a program buffer of the
file stream to the actual file (i.e) the data will be copied from the program buffer to the
operating system buffer (File)
Syntax :fileObject.flush ( )
Program
file = open (“File1.txt”, “w”)
file.write(“Writing into a file and flushing”)
file.flush( )
file.close ( )

2. fileno ( )
This method returns the integer file descriptor(file no.) which is used to request I/O
operations from the OS.
Syntax :fileObject.fileno ( )
Program
file = open (“File1.txt”, “w”)
print (“Name of file :”, f.name)
filedes=file.fileno( )
print(“File Descriptor :”,filedes)
file.close ( )
Output
Name of file : File1.txt
File Descriptor : 3

3. isatty ()

The isatty( ) method returns True, if the file is connected or associated with a
terminal device to atty device, else it returns False.
Syntax :fileObject.isatty ( )
Program
file = open (“File1.txt”, “w”)
print (“Name of file :”, f.name)
return = file.isatty( )
print(“Return value:”, return)
file.close ( )
Output
Name of file : File1.txt
Return value: False

4. seek ( )
seek ( ) method is used to set the position of the file pointer or in simpler
terms, move the file pointer to a new location in a file.
Syntax : fileObject.seek(offset[, from])
The offset argument indicates the number of bytes to be moved and the from
argument specifies the reference position from where the bytes are to be
moved.
3 possible values of from reference position are
0: sets the reference point at the beginning of the file 1: sets the reference point at the
current file position 2: sets the reference point at the end of the file

Program
fo = open("foo.txt", "r+")
print ("Name of the file: ", fo.name)

# Assuming file has following 5 lines


# This is 1st line
# This is 2nd lin
e# This is 3rd line
# This is 4th line
# This is 5th line
line = fo.readline()
print ("Read Line: %s" % (line))
# Again set the pointer to the beginningfo.seek(0, 0)
line = fo.readline()
print "Read Line: %s" % (line)
# Close filefo.close()

Output
Name of the file: foo.txt Read Line: This is 1st line.Read Line: This is 1st line.

5. tell ( ) method
The tell ( ) method returns the current position of the file read / writepointer within
the file.
It confirms that the current file position has been moved.

Syntax :
fileObject.tell (offset[, from] )

Program
# Open a file
fo= open("foo.txt","rw+") print"Name of the file: ", fo.name

# Assuming file has following 5 lines


# This is 1st line
# This is 2nd line
# This is 3rd line
# This is 4th line
# This is 5th line
line =fo.readline()
print(Read Line: %s"%(line))
# Get the current position of the file.pos=fo.tell()
print"Current Position: %d"%(pos)
# Close opened filefo.close()

Output
Name of the file: foo.txt Read Line: This is 1st lineCurrent Position: 28

6. truncate ( )
The truncate( ) method truncates the file‘s size. It resizes the file stream to size bytes.
If size is not specified, it resizes to current location. The size defaults to the current
position and they remain unchanged.

Syntax

fileObject.truncate ( [size] )
size : It is an optional argument if present, the file is truncated to the given size
Program
f = open("demofile2.txt", "a")f.truncate(20)
f.close()
#open and read the file after the truncate:
f = open("demofile2.txt", "r")print(f.read())
Output
Hello! Welcome to py

Reading text from file and splitting into words


Python allows reading line(s) from a file and splits the line (treated as a string)based on
a character.
By default, this character is space but can specify any other character to splitwords in
the string.

Program
with open(―File.txt‖,‖r‖) as file:
line = file.readline()
word = line.split()
print(words)
Output
[‗Hello‘, ‗World‘, ‗Welcome‘, ‗to‘ , ‗the‘
‗world‘,‘of‘,‘Python‘,‘Programming‘]
Format operator

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 do that is with str:
>>> x = 52
>>> fout.write(str(x))
An alternative is to use the format operator, %. When applied to integers, % is the
modulus operator. But when the first operand is a string, % is the format operator.
The first operand is the format string, which contains one or more format sequences,
which specify how the second operand is formatted. The result is a string.
For example, the format sequence '%d' means that the second operand should be
formatted as an integer (d stands for ―decimal‖):
>>> camels = 42
>>> fout.write ('%d' % camels)
'42'
The result is the string '42', which is not to be confused with the integer value 42.
A format sequence can appear anywhere in the string, so you can embed a value in a
sentence:
>>> camels = 42
>>> fout.write('I have spotted %d camels.' %camels)
'I have spotted 42 camels.'
If there is more than one format sequence in the string, the second argument has to be a
tuple. Each format sequence is matched with an element of the tuple, in order.

The following example uses '%d' to format an integer, '%g' to format a floating-point
number and '%s' to format a string:
>>> fout.write ( 'In %d years I have spotted %g %s.' % (3, 0.1, 'camels'))
'In 3 years I have spotted 0.1 camels.'

The number of elements in the tuple has to match the number of format sequences in
the string. Also, the types of the elements have to match the format sequences:
>>> fout.write ( '%d %d %d' % (1, 2)
TypeError: not enough arguments for format string
>>> fout.write ('%d' % 'dollars')
TypeError: illegal argument type for built-in operation

Renaming and Deleting files


The os module in Python has various methods that can be used to perform file-
processing operations like renaming and deleting files.
To use the methods defined in the os module,
 import os module in program
 call any related functions

The rename() Method: The rename() method takes two arguments, the current filename
and the new filename.
Its syntax is:
os.rename(old_file_name,new_file_name)

Program
import os
os.rename(“ File1.txt”, “ Students.txt”)
print (“ File Renamed”)
Output
File Renamed

The remove() Method: This method can be used to delete file(s). The method takes a
filename (name of the file to be deleted) as an argument and deletes that file.
Its syntax is:
os.remove (file_name)

Program
import os
os.remove(“Sudents.txt”)
print (“ File Deleted”)
Output
File Deleted

COMMAND LINE ARGUMENT:

program when they are started to execute.


with Python need sys module.
about constants, functions and methods of the
pyhton interpretor.
argv[ ] is used to access the command line argument. The argument list starts from 0.
sys.argv[0]= gives file name
sys.argv[1]=provides access to the first input
 Example 1 – demo.py

import sys
print(―the file name is %s‖ %(sys.argv[0]))
Output:
python demo.py
the file name is demo.py
 Example 2- addition of two num

import sys
a= sys.argv[1]
b= sys.argv[2]
sum=int(a)+int(b)
print("sum is",sum)
Output:
sam@sam~$ python sum.py 2 3
sum is 5

 Example 3- Word count using comment line arg:

from sys import argv


a = argv[1].split()
dict = {}
for i in a:
if i in dict:
dict[i]=dict[i]+1
else:
dict[i] = 1
print(dict)
print(len(a))
Output:
C:\Python34>python word.py
"python is awesome lets program in
python"
{'lets': 1, 'awesome': 1, 'in': 1, 'python': 2,'program‘:1,‘is‘:1}

MODULES

Modules are pre-written piece of code that are used to perform common task like
performing mathematical operation, generating random number. Modules are used to
break down complex programs into small manageable andorganized files

 A module is a file containing Python definitions, functions, classes and


statements with extension .py
 A module can contain executable statements as well as function definitions.
 These statements are intended to initialize the module.
 They are executed only the first time the module name is encountered in an
import statement; they also run if the file is executed as a script.
 Each module has its own private symbol table, which is used as the global
symbol table by all functions defined in the module.
 Modules can import other modules. It is customary but not required to place all
import statements at the beginning of a module. The imported module names are
placed in the importing module‘s global symbol table.
 Within a module, the module‘s name (as a string) is available as the value of the
global variable name .

CREATING AND USING USER DEFINED MODULES

1. To create a module , python code containing definitions of variables,functions and


classedis saved in a file with the file extension .py

Example:

Save the module code in a file named mymodule.py

def greeting(name):
print("Hello, " + name)

2. Use a Module: To use the module , the import statement is used in the pythonscript
where it is needed.

Example:
#Import the module named mymodule, and the greeting function is called

import mymodule
mymodule.greeting("Akash")

Output:
Hello, Akash
Note:The syntax to use a function from a module

module_name.function_name.

IMPORT STATEMENT

1. import <module name> statement


Modules can be imported from other modules using import command instead of
copying their definitions and statements into different programs.

2. from <module name> import name1,,name2[,….nameN]] statement

All the variables and function in a module can be used into another program, but to use
only selected function or variable in a module from import statement is used

Example:
from math import pi,sqrt
print(pi)
print(sqrt(25))

Output:
3.14159265359
5.0

3. from <module name>import *

To import all the function and variable in a module import * is used

Example:
from math import *
a=25
print(pi)
print(sqrt(a))

Output:
3.14159265359
5.0

4. import <module_name> as new_name where new_name is user_definedname

To import module with alias name using as a keyword

from math import sqrt as s


print(s(25))

Output:
5.0

Variables in Module:
The module contains functions, and also variables of all types (arrays, dictionaries,
objects etc):

Example:

Save this code in the file mymodule.py


person1 = {"name": "John","age": 36, "country": "Norway"}
Import the module named mymodule, and access the person1 dictionary:

import mymodule
a = mymodule.person1["age"]
print(a)

Output:
36

dirfunction:
dir( ) is a built-in function that lists the identifiers defined in a module. These identifiers
may include functions, classes and variables.
If no name is specified, the dir() will return the list of names defined in the current
module.

Example:

import math
x = dir(math)
print(x)

Output:
[' doc ', ' name ', ' package ', 'acos', 'acosh',
'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil',
'copysign', 'cos', 'cosh', 'degrees', 'e', 'exp', 'fabs',
'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'hypot',
'isinf', 'isnan', 'ldexp', 'log', 'log10', 'log1p',
'modf', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt',
'tan', 'tanh', 'trunc']
BUILT-IN MODULES IN PYTHON
1. math — Mathematical functions

This module provides access to the mathematical functions defined by the Cstandard.
These functions cannot be used with complex numbers; use the functions of the same
name from the cmath module to support for complex numbers.
The following are some functions provided by this module. Except when explicitly
noted otherwise, all return values are floats.

Number-theoretic and representation functions


1. math.ceil(x) - Return the ceiling of x, the smallest integer greater than or equal to x. If
x is not a float, delegates to x.ceil(), which should return an Integral value.
2. math.fabs(x) - Return the absolute value of x.
3. math.factorial(x) -Return x factorial as an integer. Raises ValueError if x is not
integral or is negative.
4. math.log(x[, base]) - With one argument, return the natural logarithm of x (to base e).
With two arguments, return the logarithm of x to the given base, calculated as
log(x)/log(base).
5. math.sqrt(x) - Return the square root of x.
6. math.pow(x, y) -Return x raised to the power y.
7. math.dist(p, q) - Return the Euclidean distance between two points p and q, each
given as a sequence (or iterable) of coordinates. The two points must have the same
dimension.
8. math.degrees(x) - Convert angle x from radians to degrees.
9. math.radians(x) - Convert angle x from degrees to radians.Constants in math
10.math.pi -The mathematical constant π = 3.141592…, to available precision.
11.math.e - The mathematical constant e = 2.718281…, to available precision.

2. datetime — Basic date and time types

The datetime module supplies classes for manipulating dates and times.

While date and time arithmetic is supported, the focus of the implementation is on
efficient attribute extraction for output formatting and manipulation.

1. datetime.date - An idealized naive date, assuming the current Gregorian calendar


always was, and always will be, in effect. Attributes: year, month,and day.
2. datetime.time - An idealized time, independent of any particular day, assumingthat
every day has exactly 24*60*60 seconds.
Attributes: hour, minute, second, microsecond, and tzinfo.
3.datetime.datetime - A combination of a date and a time.
Attributes: year, month, day, hour, minute, second, microsecond, and tzinfo.
3. calendar module— General calendar-related functions

1. calendar.isleap(year) - Returns True if year is a leap year, otherwise False.


2. calendar.leapdays(y1, y2) -Returns the number of leap years in the range from y1 to
y2 (exclusive), where y1 and y2 are years. This function works for ranges spanning a
century change.

4. Random module-This module implements pseudo-random number generators for


various distributions.

1. random.randrange(stop):Return a randomly selected integer element from range


(0, stop).

>>>random.randrange(88)17
2. random.randrange(start,stop[,step]) : Return a randomly selected integer
element from range (start, stop, step).

>>>random.randrange(3,100,5)
83
3. random.choice(seq) :Return a random element from the non-empty sequence seq.
If seq is empty, raises IndexError.

>>>random.choice('abcdefghij')' e '
>>>random.choice(['aa','bb','cc',11,22])' cc'

PACKAGES

Package is a collection of modules that are related to each.


A package is a hierarchical file directory structure that has modules and other packages
within it.
A package is a directory of Python modules that contains an additional init .py
file, which distinguishes a package from a directory that is supposed to contain
multiple Python scripts.
Packages can be nested to multiple depths if each corresponding directory contains its
own init .py file.

STEPS TO CREATE A PYTHON PACKAGE :

1. Create a directory and insert package's name.


2. Insert necessary classes into it.
3. Create a init .py file in the directory
Example
The mod1.py and mod2.py are stored in pkg package.

mod1.py

def a():
print('Hello')

mod2.py

def b():
print('Everyone')

import pkg.mod1, pkg.mod2


print(pkg.mod1.a())
print(pkg.mod2.b())

Output:

Hello
Everyone
Locating modules :
When a module is imported, the Python interpreter searches for the modulein the
following sequences :
1. The current directory
2. If the module is not found, Python then searches each directory in the shell
variable PYTHONPATH environment variable (a list of directory
names, with the same syntax as the shell variable PATH).
3. If all else fails, Python checks the default path.
Errors and Exceptions

Errors are the problems in a program due to which the program will stop theexecution.

On the other hand, exceptions are raised when some internal events occur which
changes the normal flow of the program.

Two types of Error occur in python.

1. Syntax errors
2. Logical errors

Syntax errors
 Syntax errors are detected when the rules of the particular programming language
are not followed while writing a program.
 These errors are also known as parsing errors.
 On encountering a syntax error, the Python interpreter does not execute the program
unless the errors are rectified.
 When a syntax error is encountered while working in shell mode, Python displays
the name of the error and a small description about the error as shown in Example.

Example

# initialize the amount variableamount =10000


# check that You are eligible to
# purchase order Placed or not

if(amount>2999)
print("You are eligible to purchase ")
Output:

It returns a syntax error message because after in if statement a colon : is missing. This
can be fixed by writing the correct syntax.

Logical errors

Logic error specifies all those type of errors in which the program executes but gives
incorrect results.

particular program
dividedby zero then ZeroDivisionError exception is
raised, or when a module is imported that does not exist then ImportError is raised.
-time error that causes the program to
terminate abruptly. These types of run-time errors are known as exceptions.

Example :
# initialize the amount variable
marks = 10000

# perform division with 0


a = marks / 0
print(a)

Output:

In the above example, the ZeroDivisionError occurs as a number is divided by 0.


Exceptions
 Even if a statement or expression is syntactically correct, there might arise an error
during its execution.
 For example, trying to open a file that does not exist, division by zero and so on.
Such types of errors might disrupt the normal execution of the program and are
called exceptions.
 An exception is a Python object that represents an error.
 When an error occurs during the execution of a program, an exception is said to
have been raised.
 Such an exception needs to be handled by the programmer so that the program
does not terminate abnormally.
 Therefore, while designing a program, a programmer may anticipate such
erroneous situations that may arise during its execution and can address them by
including appropriate code to handle that exception.
Built-in Exceptions
 Commonly occurring exceptions are usually defined in the compiler/interpreter.
These are called built-in exceptions.
 Python‘s standard library is an extensive collection of built-in exceptions that deals
with the commonly occurring errors (exceptions) by providing the standardized
solutions for such errors.
 On the occurrence of any built-in exception, the appropriate exception handler code
is executed which displays the reason along with the raised exception name. The
programmer then has to take appropriate action to handle it.
 Some of the commonly occurring built-in exceptions that can be raised inPython are
Exception Description
IndexError When the wrong index of a list is retrieved.
AssertionErr It occurs when assert statement fails
or
AttributeErro It occurs when an attribute assignment is failed.
r
ImportError It occurs when an imported module is not found.
KeyError It occurs when the key of the dictionary is not found.
NameError It occurs when the variable is not defined.
MemoryError It occurs when a program run out of memory.
It occurs when a function and operation is applied in an incorrect
TypeError type.

Exception Handling
 Exception handling is a useful technique that helps in capturing runtime errors and
handling them so as to avoid the program getting crashed.
 Python categorizes exceptions into distinct types so that specific exception handlers
(code to handle that particular exception) can be created for each type.
 Exception handlers separate the main logic of the program from the error detection
and correction code.
 The segment of code where there is any possibility of error or exception, is placed
inside one block. The code to be executed in case the exception has occurred, is
placed inside another block.
 The compiler or interpreter keeps track of the exact position where the error has
occurred.
 Exception handling can be done for both user-defined and built-inexceptions.
i) Handling Exceptions with try/except

 Exceptions in a program is handled by using try block and except block.


 A critical operation which can raise exception is placed inside the try block and the
code that handles exception is written in except block.
 The syntax for try–except block can be given as,

try:
statements
except ExceptionName:
statements

Example :
try:
numerator=50
denom=int(input("Enter the denominator"))
quotient=(numerator/denom)
print ("Division performed successfully")
except ZeroDivisionError:
print ("Denominator as ZERO.... not allowed")
print(―OUTSIDE try..except block‖)

Output

Enter the denominator 0


Denominator as ZERO.... not allowed
OUTSIDE try..except block

ii) Multiple except blocks

 Sometimes, a single piece of code might be suspected to have more than one type
of error.
 For handling such situations, Python allows multiple except blocks for a single try
block.
 The block which matches with the exception generated will get executed.
 A try block can be associated with more than one except block to specify handlers
for different exceptions. However, only one handler will be executed.
 Exception handlers only handle exceptions that occur in the corresponding try
block.
 The syntax for specifying multiple except blocks for a single try block,
try:
statements
except Exception1:
If there is Exception1, then execute this block
except Exception2:
If there is Exception2, then execute this block
else :
If there is no Exception, then execute this
block

Example

try:
numerator=50
denom=int(input("Enter the denominator: "))
print (numerator/denom)
print ("Division performed successfully")
except ZeroDivisionError:
print ("Denominator as ZERO is not allowed")
except ValueError:
print ("Only INTEGERS should be entered")

iii) Multiple Exceptions in a Single Block

When code in except block is the same for multiple exceptions being caught then
multiple exceptions can be handled in a single except block.
Example
try:
num=int(input(“Enter the number:”))
print (num**2)
except (ValueError,TypeError, KeyboardInterrupt):
print(“Please check before you enter… Prog
Terminating”)
print(„outside try block‟)

Output

Enter the number:abc


Please check before you enter… ProgTerminating
outside try block

iv. Except block without exception


 However, if an exception is raised for which no handler is created by the
programmer, then such an exception can be handled by adding an except clause
without specifying any exception. This except clause should be added as the last
clause of the try..except block
 The syntax to define a handler that would catch every possible exception from the
try block is
try:
statements
except ExceptionName:
statements
except:
statements

Example:

try:
numerator=50
denom=int(input("Enter the denominator"))
quotient=(numerator/denom)
print ("Division performed successfully")
except ValueError:
print ("Only INTEGERS should be entered")
except:
print(" OOPS.....SOME EXCEPTION RAISED")

v. try...except…else clause

 The try ... except block can optionally have an else clause, which,when present, must
follow all except blocks.
 The statement(s) in the else block is executed only if the try clause does not raise an
exception.
Syntax

try:
# Some Code
except:
# Executed if error in the
# try block
else:
# execute if no exception

Example
try:
file=open(‗File1.txt‘)str=file.readline()print(str)
except IOError:
print(―Error occurred during input…‖)
else:
print(―Program executed successfully‖)

Output 1 Output 2

Program executedsuccessfully Error occurred during


input…

vi) try and finally Block

 The try statement in Python can also have an optional finally clause.
 The statements inside the finally block is always executed regardless of whether an
exception has occurred in the try block or not.
 It is a common practice to use finally clause while working with files to ensure that
the file object is closed.
 If used, finally should always be placed at the end of try clause, after all except
blocks and the else block irrespective of whether an exception has occurred or not.
 The syntax of finally block can be given as,

try:
Write your operations here
......................
Due to any exception, operations written here
will be skipped
finally:
This would always be executed.
......................

Example

try:
numerator=50
denom=int(input("Enter the denominator: "))
quotient=(numerator/denom)
print ("Division performed successfully")
except ZeroDivisionError:
print ("Denominator as ZERO is not allowed")
except ValueError:
print ("Only INTEGERS should be entered")
else:
print ("The result of division operation is ",quotient)
finally:
print ("In finally block")

Output 1 Output 2
Enter the denominator:2Division Enter the denominator: 0 Denominator
performed successfully as ZERO is notallowed
The result of divisionoperation is 25 In finally block
In finally block

Handling Exceptions in Invoked Functions


 Exceptions can be handled inside the functions that are called in the try block.
 When the function invoked by the try block throws an exception which is handled
by the except block in the calling function
Syntax
function_name(arg list):

try:

function_name( ) //function call


except ExceptionName:

// Code to handle exception


Example:

def divide(num,deno):
try:
Quo=num/deno
except ZeroDivisionError:
print(“Cannot divide by a number by zero”)
divide(10,0)

Output
Cannot divide by a number by zero

Raising Exceptions

 Programmers can also forcefully raise exceptions in a program using the raise and
assert statements.
 Once an exception is raised, no further statement in the current block of code is
executed.
 So, raising an exception involves interrupting the normal flow execution of program
and jumping to that part of the program (exception handler code) which is written
to handle such exceptionalsituations.
1. raise statement

 The raise statement can be used to throw an exception.


 The syntax of raise statement is:

raise exception-name[(optional argument)]


The argument is generally a string that is displayed when the exception is raised.

Example

try:
num=10
print(num)
raise ValueError
except:
print(“Execution occurred….Prog terminating”)
Output

10
Execution occurred….Prog terminating

2. assert statement (Assertions in Python)


 An assert statement in Python is used to test an expression in the programcode.If
the result after testing isFalse, then the Assertion exception is raised.
 This statement is generally used in the beginning of the function or after a function
call to check for valid input.
 The syntax for assert statement is:

assert Expression[,arguments]

 On encountering an assert statement, Python evaluates the expressiongiven


immediately after the assert keyword.
 If this expression is false, an AssertionError exception is raised which can be
handled like any other exception

Example

print("use of assert statement")


def negativecheck(number):
assert(number>=0), "Negative Number"
print(number*number)
print (negativecheck(100))
print (negativecheck(-350))

Output
10000
Negative Number

You might also like