Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
13 views42 pages

Python PDF 4

The document provides an overview of Python file objects, detailing their types (text, binary, and raw files) and associated methods and attributes for file manipulation. It also covers command line arguments handling using sys, getopt, and argparse modules, as well as file system operations with the os module. Additionally, it discusses persistent storage options in Python, including the pickle and shelve modules for object serialization, and highlights common exceptions encountered during runtime.
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)
13 views42 pages

Python PDF 4

The document provides an overview of Python file objects, detailing their types (text, binary, and raw files) and associated methods and attributes for file manipulation. It also covers command line arguments handling using sys, getopt, and argparse modules, as well as file system operations with the os module. Additionally, it discusses persistent storage options in Python, including the pickle and shelve modules for object serialization, and highlights common exceptions encountered during runtime.
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/ 42

What is the File Object?

Python file object provides methods and attributes to access and manipulate files. Using
file objects, we can read or write any files.

Whenever we open a file to perform any operations on it, Python returns a file object. To
create a file object in Python use the built-in functions, such as open() and os.popen().

IOError exception is raised when a file object is misused, or file operation fails for an I/O-
related reason.

Types of File Object

In Python, there are three different categories of a file object, which are listed below:

1. Text files
2. Binary files
3. Raw files

Text files (TextIOWrapper)

The text file type is most common. Usually, we use text files to store character data or storing
information in plain text with no special formatting beyond basic fonts and font styles.

We open a text file using the open() function. For example, open('test'.txt', 'r'). When we open
a text file, it returns a TextIOWrapper file object.

Example

file = open('test.txt', 'w')


print(type(file))
# Output: <class '_io.TextIOWrapper'>

Binary Files (BufferedReader and BufferedWriter)

Data is stored on a disk in the form of binary. For example, we use binary files to store data
like images or videos. Binary files are a computer-readable form of storing data.

A program is needed to interpret the data in a binary file and display it to the user. The binary
files are also called buffered files. This file type is used for reading and writing binary data.
Open the binary files using the open() function in binary mode. For example, open('abc.txt',
'rb'). It opens the file to read-only in binary mode. The file pointer exists at the beginning of
the file.

The open() function will return the BufferedReader when we open the binary file for reading
and the BufferedWriter file object when we open a binary file for writing.

Example

file = open('test.txt', 'rb')


print(type(file))
# Output: <class '_io.BufferedReader'>

Raw Files

A raw file is a collection of unprocessed data. It means the raw file has not been altered or
manipulated in any way by the computer.

The raw files are also called unbuffered files, and this file type is generally used as a low-
level building block for binary and text streams. Mostly, The raw file is not used.

When we open these files, using the open() function will return a FileIO object.

Example

file = open('test.txt', 'rb', buffering=0)


print(type(file))
# Output: <class '_io.FileIO'>

The raw files are also called unbuffered files, and this file type is generally used as a low-
level building block for binary and text streams. Mostly, The raw file is not used.

When we open these files, using the open() function will return a FileIO object.

Example

file = open('test.txt', 'rb', buffering=0)


print(type(file))
# Output: <class '_io.FileIO'>

 name: Return the name of the file. It is a read-only attribute and may not be
present on all file-like objects. If the file object was created using
the open() function, the file’s name is returned. Otherwise, some string
indicates the source of the file object is returned.
 encoding: It returns the encoding this file uses, such as UTF-8. This attribute is
read-only. When Unicode strings are written to a file, they will be converted to
byte strings using this encoding. It may also be None. In that case, the file uses
the system default encoding for converting Unicode strings.
 mode: Returns the file access mode used while opening a file.
 closed: Returns True if a file is closed. It is a boolean value indicating the
current state of the file object.
 newline: Files opened in universal newline read mode keep track of the
newlines encountered while reading the file. The values are ‘\r’, ‘\n’, ‘\r\n’,
None (no newlines read yet), or a tuple containing all the newline types seen.
For files not opened in universal newline read mode, the value of this attribute
will be None
Example:

with open(r'E:\pynative\files\test.txt', "r") as fp:


print('Is Closed:', fp.closed)
print('Encoding Used:', fp.encoding)
print('Access Mode:', fp.mode)
print('NewLines Found:', fp.newlines)

File Object Methods

File object has the following methods that we can use to accessing a file: A file can be
opened with a built-in function called open(). This function takes in the file’s path and the
access mode and returns a file object.

Method Description

read() Returns the file content.

readable() Returns whether the file stream can be read or not.

readline() Read single line

readlines() Read file into a list

truncate(size) Resizes the file to a specified size.


Method Description

writable() Returns whether the file can be written to or not.

write() Writes the specified string to the file.

writelines() Writes a list of strings to the file.

close() Closes the opened file.

seek() Set file pointer position in a file

seekable() Returns whether the file allows us to change the file position.

tell() Returns the current file location.

detach() Returns the separated raw stream from the buffer

fileno() Returns a number that represents the stream, from the operating system’s perspective.

flush() Flushes the internal buffer.

isatty() Returns whether the file stream is interactive or not.


ATTRIBUTES OF A FILE OBJECT
 close() - Close the file.
 next() - When a file is used as an iterator, typically in a for loop (for example,
for line in f: print line), the next() method is called repeatedly. This method
returns the next input line, or raises StopIteration when EOF is hit.
 read([size]) - Read at most size bytes from the file.
 readline([size]) - Read one entire line from the file.
 seek(offset[, whence]) - Set the file's current position, like stdio's fseek(). The
whence argument is optional and defaults to 0 (absolute file positioning); other
values are 1 (seek relative to the current position) and 2 (seek relative to the file's
end).
 tell() - Return the file's current position, like stdio's ftell().
 write(str) - Write a string to the file.
 writelines(sequence) - Write a sequence of strings to the file.
Following are file object's most used attributes −
 closed - bool indicating the current state of the file object.
 encoding - The encoding that this file uses.
 mode - The I/O mode for the file.
 name - If the file object was created using open(), the name of the file.
Otherwise, some string that indicates the source of the file object

Command Line Arguments


The arguments that are given after the name of the program in the command line shell of
the operating system are known as Command Line Arguments. Python provides various
ways of dealing with these types of arguments. The three most common are:
 Using sys.argv
 Using getopt module
 Using argparse module
The sys module provides functions and variables used to manipulate different parts of the
Python runtime environment. This module provides access to some variables used or
maintained by the interpreter and to functions that interact strongly with the interpreter.
One such variable is sys.argv which is a simple list structure. It’s main purpose are:
 It is a list of command line arguments.
 len(sys.argv) provides the number of command line arguments.
 sys.argv[0] is the name of the current Python script.
USING GETOPT MODULE
Python getopt module is similar to the getopt() function of C. Unlike sys module getopt
module extends the separation of the input string by parameter validation. It allows both
short, and long options including a value assignment. However, this module requires the
use of the sys module to process input data properly. To use getopt module, it is required
to remove the first element from the list of command-line arguments.

Syntax: getopt.getopt(args, options, [long_options])


Parameters:
args: List of arguments to be passed.
options: String of option letters that the script want to recognize. Options that require an
argument should be followed by a colon (:).
long_options: List of string with the name of long options. Options that require arguments
should be followed by an equal sign (=).
Return Type: Returns value consisting of two elements: the first is a list of (option, value)
pairs. The second is the list of program arguments left after the option list was stripped.

USING ARGPARSE MODULE


Using argparse module is a better option than the above two options as it provides a lot of
options such as positional arguments, default value for arguments, help message,
specifying data type of argument etc.

Note: As a default optional argument, it includes -h, along with its long version –help.

File System
In python, the file system contains the files and directories. To handle these files and directories
python supports “os” module. Python has the “os” module, which provides us with many useful
methods to work with directories (and files as well).
The os module provides us the methods that are involved in file processing operations and
directory processing like renaming, deleting, get current directory, changing directory etc.

Renaming the file - rename()

The os module provides us the rename() method which is used to rename the specified file to
a new name.
Syntax:

os.rename (“current-name”, “new-name”)

Example:
import os;
#rename file2.txt to file3.txt
os.rename("file2.txt","file3.txt")
Removing the file – remove()

The os module provides us the remove() method which is used to remove the specified file.
Syntax:

os.remove(“file-name”)

Example:
import os;
#deleting the file named file3.txt
os.remove("file3.txt")

Creating the new directory – mkdir()

The mkdir() method is used to create the directories in the current working directory.
Syntax:

os.mkdir(“directory-name”)

Example:
import os;
#creating a new directory with the name new
os.mkdir("dirnew")

Changing the current working directory – chdir()

The chdir() method is used to change the current working directory to a specified directory.
Syntax:

os.chdir("new-directory")

Example:
import os;
#changing the current working directory to new
os.chdir("dir2")

Get current working directory – getpwd()

This method returns the current working directory.


Syntax:

os.getcwd()

Example:
import os;
#printing the current working directory
print(os.getcwd())

Deleting directory - rmdir()

The rmdir() method is used to delete the specified directory.


Syntax:

os.rmdir(“directory name”)

Example:
import os;
#removing the new directory
os.rmdir("dir2")

List Directories and Files – listdir()

All files and sub directories inside a directory can be known using the listdir() method. This
method takes in a path and returns a list of sub directories and files in that path. If no path is
specified, it returns from the current working directory.
Syntax:

os.listdir([“path”])

Example:
import os;
#list of files and directories in current working directory
print(os.listdir())
#list of files and directories in specified path
print(os.listdir(“D:\\”))
Persistent Storage Modules
In many of the exercises in this text, user input is required. After many iterations, it may be
somewhat frustrating being required to enter the same data repeatedly. The same may occur if
you are entering a significant amount of data for use in the future. This is where it becomes
useful to have persistent storage, or a way to archive your data so that you may access them
at a later time instead of having to re-enter all of that information. When simple disk files are
no longer acceptable and full relational database management systems (RDBMSs) are
overkill, simple persistent storage fills the gap. The majority of the persistent storage modules
deals with storing strings of data, but there are ways to archive Python objects as well.

9.9.1. pickle and marshal Modules


Python provides a variety of modules that implement minimal persistent storage. One set of
modules (marshal and pickle) allows for pickling of Python objects. Pickling is the process
whereby objects more complex than primitive types can be converted to a binary set of bytes
that can be stored or transmitted across the network, then be converted back to their original
object forms. Pickling is also known as flattening, serializing, or marshalling. Another set of
modules (dbhash/bsddb, dbm, gdbm, dumbdbm) and their "manager" (anydbm) can provide
persistent storage of Python strings only. The last module (shelve) can do both.

As we mentioned before, both marshal and pickle can flatten Python objects. These modules
do not provide "persistent storage" per se, since they do not provide a namespace for the
objects, nor can they provide concurrent write access to persistent objects. What they can do,
however, is to pickle Python objects to allow them to be stored or transmitted. Storage, of
course, is sequential in nature (you store or transmit objects one after another). The difference
between marshal and pickle is that marshal can handle only simple Python objects (numbers,
sequences, mapping, and code) while pickle can transform recursive objects, objects that are
multi-referenced from different places, and user-defined classes and instances.
The pickle module is also available in a turbo version called cPickle, which implements all
functionality in C.

9.9.2. DBM-style Modules


The *db* series of modules writes data in the traditional DBM format. There are a large
number of different implementations: dbhash/bsddb, dbm, gdbm, and dumbdbm. If you are
particular about any specific DBM module, feel free to use your favorite, but if you are not
sure or do not care, the generic anydbm module detects which DBM-compatible modules are
installed on your system and uses the "best" one at its disposal. The dumbdbm module is the
most limited one, and is the default used if none of the other packages is available. These
modules do provide a namespace for your objects, using objects that behave similar to a
combination of a dictionary object and a file object. The one limitation of these systems is
that they can store only strings. In other words, they do not serialize Python objects.

9.9.3. shelve Module


Finally, we have a somewhat more complete solution, the shelve module. The shelve module
uses the anydbm module to find a suitable DBM module, then uses cPickle to perform the
pickling process. The shelve module permits concurrent read access to the database file, but
not shared read/write access. This is about as close to persistent storage as you will find in the
Python standard library. There may be other external extension modules that implement
"true" persistent storage. The diagram in Figure 9-1 shows the relationship between the
pickling modules and the persistent storage modules, and how the shelve object appears to be
the best of both worlds.

Figure 9-1. Python modules for serialization and persistency

Core Module: pickle and cPickle

The pickle module allows you to store Python objects directly to a file
without having to convert them to strings or to necessarily write them out
as binary files using low-level file access. Instead, the pickle module
creates a Python-only binary version that allows you to cleanly read and
write objects in their entirety without having to worry about all the file
details. All you need is a valid file handle, and you are ready to read or
write objects from or to disk.

The two main functions in the pickle module are dump() and load().
The dump() function takes a file handle and a data object and saves the
object in a format it understands to the given file. When a pickled object is
loaded from disk using load() , it knows exactly how to restore that object
to its original configuration before it was saved to disk. We recommend
you take a look at pickle and its "smarter" brother, shelve, which gives you
dictionary-like functionality so there is even less file overhead on your
part. cPickle is the faster C-compiled version of pickle

Exceptions in Python

Python Logical Errors (Exceptions)

Errors that occur at runtime (after passing the syntax test) are called exceptions or logical
errors.
For instance, they occur when we

 try to open a file(for reading) that does not exist (FileNotFoundError)


 try to divide a number by zero (ZeroDivisionError)
 try to import a module that does not exist (ImportError) and so on.
Whenever these types of runtime errors occur, Python creates an exception object.

If not handled properly, it prints a traceback to that error along with some details about why
that error occurred.

Let's look at how Python treats these errors:

divide_numbers = 7 / 0
prit(divide_numbers)
Run Code

Output

Traceback (most recent call last):


File "<string>", line 1, in <module>
ZeroDivisionError: division by zero

Here, while trying to divide 7 / 0, the program throws a system exception ZeroDivisionError
Python Built-in Exceptions

Illegal operations can raise exceptions. There are plenty of built-in exceptions in Python that
are raised when corresponding errors occur.We can view all the built-in exceptions using the
built-in local() function as follows:

print(dir(locals()['__builtins__']))
Run Code

Here, locals()['__builtins__'] will return a module of built-in exceptions, functions, and


attributes and dir allows us to list these attributes as strings.
Some of the common built-in exceptions in Python programming along with the error that
cause them are listed below:

Exception Cause of Error

AssertionError Raised when an assert statement fails.

AttributeError Raised when attribute assignment or reference fails.

EOFError Raised when the input() function hits end-of-file condition.

FloatingPointError Raised when a floating point operation fails.

GeneratorExit Raise when a generator's close() method is called.

ImportError Raised when the imported module is not found.

IndexError Raised when the index of a sequence is out of range.

KeyError Raised when a key is not found in a dictionary.

KeyboardInterrupt Raised when the user hits the interrupt key (Ctrl+C or Delete).

MemoryError Raised when an operation runs out of memory.

NameError Raised when a variable is not found in local or global scope.

NotImplementedError Raised by abstract methods.


OSError Raised when system operation causes system related error.

Raised when the result of an arithmetic operation is too large to


OverflowError
be represented.

Raised when a weak reference proxy is used to access a garbage


ReferenceError
collected referent.

RuntimeError Raised when an error does not fall under any other category.

Raised by next() function to indicate that there is no further item


StopIteration
to be returned by iterator.

SyntaxError Raised by parser when syntax error is encountered.

IndentationError Raised when there is incorrect indentation.

TabError Raised when indentation consists of inconsistent tabs and spaces.

SystemError Raised when interpreter detects internal error.

SystemExit Raised by sys.exit() function.

Raised when a function or operation is applied to an object of


TypeError
incorrect type.

Raised when a reference is made to a local variable in a function or


UnboundLocalError
method, but no value has been bound to that variable.

UnicodeError Raised when a Unicode-related encoding or decoding error occurs.

UnicodeEncodeError Raised when a Unicode-related error occurs during encoding.

UnicodeDecodeError Raised when a Unicode-related error occurs during decoding.

UnicodeTranslateError Raised when a Unicode-related error occurs during translating.

Raised when a function gets an argument of correct type but improper


ValueError
value.

Raised when the second operand of division or modulo operation is


ZeroDivisionError
zero.
Python Error and Exception

Errors represent conditions such as compilation error, syntax error, error in the logical part
of the code, library incompatibility, infinite recursion, etc.
Errors are usually beyond the control of the programmer and we should not try to handle
errors.

Exceptions can be caught and handled by the program.

Python Exception Handling

In the tutorial, we will learn about different approaches of exception handling in Python with
the help of examples.

Python try...except Block

The try...except block is used to handle exceptions in Python. Here's the syntax
of try...except block:

try:
# code that may cause exception
except:
# code to run when exception occurs

Here, we have placed the code that might generate an exception inside the try block.
Every try block is followed by an except block.
When an exception occurs, it is caught by the except block. The except block cannot be used
without the try block.
Example: Exception Handling Using try...except

try:
numerator = 10
denominator = 0

result = numerator/denominator

print(result)
except:
print("Error: Denominator cannot be 0.")
# Output: Error: Denominator cannot be 0.
Run Code

In the example, we are trying to divide a number by 0. Here, this code generates an
exception.
To handle the exception, we have put the code, result = numerator/denominator inside
the try block. Now when an exception occurs, the rest of the code inside the try block is
skipped.
The except block catches the exception and statements inside the except block are executed.
If none of the statements in the try block generates an exception, the except block is skipped.

Catching Specific Exceptions in Python

For each try block, there can be zero or more except blocks. Multiple except blocks allow us
to handle each exception differently.

The argument type of each except block indicates the type of exception that can be handled
by it. For example,
try:

even_numbers = [2,4,6,8]
print(even_numbers[5])

except ZeroDivisionError:
print("Denominator cannot be 0.")

except IndexError:
print("Index Out of Bound.")

# Output: Index Out of Bound


Run Code

In this example, we have created a list named even_numbers.


Since the list index starts from 0, the last element of the list is at index 3. Notice the
statement,
print(even_numbers[5])

Here, we are trying to access a value to the index 5. Hence, IndexError exception occurs.
When the IndexError exception occurs in the try block,
 The ZeroDivisionError exception is skipped.
 The set of code inside the IndexError exception is executed.

Python try with else clause

In some situations, we might want to run a certain block of code if the code block
inside try runs without any errors.
For these cases, you can use the optional else keyword with the try statement.
Let's look at an example:

# program to print the reciprocal of even numbers

try:
num = int(input("Enter a number: "))
assert num % 2 == 0
except:
print("Not an even number!")
else:
reciprocal = 1/num
print(reciprocal)
Run Code

Output
If we pass an odd number:

Enter a number: 1
Not an even number!

If we pass an even number, the reciprocal is computed and displayed.

Enter a number: 4
0.25
However, if we pass 0, we get ZeroDivisionError as the code block inside else is not handled
by preceding except.

Enter a number: 0
Traceback (most recent call last):
File "<string>", line 7, in <module>
reciprocal = 1/num
ZeroDivisionError: division by zero

Note: Exceptions in the else clause are not handled by the preceding except clauses.

Python try...finally

In Python, the finally block is always executed no matter whether there is an exception or
not.

The finally block is optional. And, for each try block, there can be only one finally block.

Let's see an example,

try:
numerator = 10
denominator = 0

result = numerator/denominator

print(result)
except:
print("Error: Denominator cannot be 0.")

finally:
print("This is finally block.")
Run Code

Output

Error: Denominator cannot be 0.


This is finally block.
In the above example, we are dividing a number by 0 inside the try block. Here, this code
generates an exception.
The exception is caught by the except block. And, then the finally block is executed.

Context Managers

Context managers allow you to allocate and release resources precisely when you want to.
The most widely used example of context managers is the with statement. Suppose you have
two related operations which you’d like to execute as a pair, with a block of code in between.
Context managers allow you to do specifically that. For example:

with open('some_file', 'w') as opened_file:


opened_file.write('Hola!')

The above code opens the file, writes some data to it and then closes it. If an error occurs
while writing the data to the file, it tries to close it. The above code is equivalent to:

file = open('some_file', 'w')


try:
file.write('Hola!')
finally:
file.close()

While comparing it to the first example we can see that a lot of boilerplate code is eliminated
just by using with . The main advantage of using a with statement is that it makes sure our
file is closed without paying attention to how the nested block exits.

A common use case of context managers is locking and unlocking resources and closing
opened files (as I have already shown you).

Let’s see how we can implement our own Context Manager. This should allow us to
understand exactly what’s going on behind the scenes.

27.1. Implementing a Context Manager as a Class:

At the very least a context manager has an __enter__ and __exit__ method defined. Let’s
make our own file-opening Context Manager and learn the basics.

class File(object):
def __init__(self, file_name, method):
self.file_obj = open(file_name, method)
def __enter__(self):
return self.file_obj
def __exit__(self, type, value, traceback):
self.file_obj.close()

Just by defining __enter__ and __exit__ methods we can use our new class in
a with statement. Let’s try:

with File('demo.txt', 'w') as opened_file:


opened_file.write('Hola!')

Our __exit__ method accepts three arguments. They are required by


every __exit__ method which is a part of a Context Manager class. Let’s talk about what
happens under-the-hood.

1. The with statement stores the __exit__ method of the File class.
2. It calls the __enter__ method of the File class.
3. The __enter__ method opens the file and returns it.
4. The opened file handle is passed to opened_file .
5. We write to the file using .write() .
6. The with statement calls the stored __exit__ method.
7. The __exit__ method closes the file.

Exceptions

Even if a statement or expression is syntactically correct, it may cause an error when an


attempt is made to execute it. Errors detected during execution are called exceptions and are
not unconditionally fatal: you will soon learn how to handle them in Python programs. Most
exceptions are not handled by programs, however, and result in error messages as shown
here:

>>> 10 * (1/0)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ZeroDivisionError: division by zero
>>> 4 + spam*3
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'spam' is not defined
>>> '2' + 2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can only concatenate str (not "int") to str

The last line of the error message indicates what happened. Exceptions come in different
types, and the type is printed as part of the message: the types in the example
are ZeroDivisionError, NameError and TypeError. The string printed as the exception type is
the name of the built-in exception that occurred. This is true for all built-in exceptions, but
need not be true for user-defined exceptions (although it is a useful convention). Standard
exception names are built-in identifiers (not reserved keywords).

The rest of the line provides detail based on the type of exception and what caused it.

The preceding part of the error message shows the context where the exception occurred, in
the form of a stack traceback. In general it contains a stack traceback listing source lines;
however, it will not display lines read from standard input.

Raising Exceptions

The raise statement allows the programmer to force a specified exception to occur. For
example:

>>> raise NameError('HiThere')


Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: HiThere

The sole argument to raise indicates the exception to be raised. This must be either an
exception instance or an exception class (a class that derives from BaseException, such
as Exception or one of its subclasses). If an exception class is passed, it will be implicitly
instantiated by calling its constructor with no arguments:

raise ValueError # shorthand for 'raise ValueError()'

If you need to determine whether an exception was raised but don’t intend to handle it, a
simpler form of the raise statement allows you to re-raise the exception:

>>> try:
... raise NameError('HiThere')
... except NameError:
... print('An exception flew by!')
... raise
...
An exception flew by!
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
NameError: HiThere
What is Assertion?

Assertions are statements that assert or state a fact confidently in your program. For example,
while writing a division function, you're confident the divisor shouldn't be zero, you assert
divisor is not equal to zero.

Assertions are simply boolean expressions that check if the conditions return true or not. If it
is true, the program does nothing and moves to the next line of code. However, if it's false,
the program stops and throws an error.

It is also a debugging tool as it halts the program as soon as an error occurs and displays it.

We can be clear by looking at the flowchart below:


Python assert Statement
Python has built-in assert statement to use assertion condition in the
program. assert statement has a condition or expression which is supposed to be always true.
If the condition is false assert halts the program and gives an AssertionError.
Syntax for using Assert in Pyhton:

assert <condition>

assert <condition>,<error message>

In Python we can use assert statement in two ways as mentioned above.


1. assert statement has a condition and if the condition is not satisfied the program will stop and
give AssertionError.
2. assert statement can also have a condition and a optional error message. If the condition is
not satisfied assert stops the program and gives AssertionError along with the error message.
Let's take an example, where we have a function that will calculate the average of the values
passed by the user and the value should not be an empty list. We will use assert statement to
check the parameter and if the length is of the passed list is zero, the program halts.
Example 1: Using assert without Error Message

def avg(marks):
assert len(marks) != 0
return sum(marks)/len(marks)

mark1 = []
print("Average of mark1:",avg(mark1))
Run Code

When we run the above program, the output will be:

AssertionError

We got an error as we passed an empty list mark1 to assert statement, the condition became
false and assert stops the program and give AssertionError.
Now let's pass another list which will satisfy the assert condition and see what will be our
output.
Example 2: Using assert with error message

def avg(marks):
assert len(marks) != 0,"List is empty."
return sum(marks)/len(marks)

mark2 = [55,88,78,90,79]
print("Average of mark2:",avg(mark2))

mark1 = []
print("Average of mark1:",avg(mark1))
Run Code

When we run the above program, the output will be:

Average of mark2: 78.0


AssertionError: List is empty.

We passed a non-empty list mark2 and also an empty list mark1 to the avg() function and we
got output for mark2 list but after that we got an error AssertionError: List is empty.
The assert condition was satisfied by the mark2 list and program to continue to run.
However, mark1 doesn't satisfy the condition and gives an AssertionError

Exception Name &


Description

1
Exception
Base class for all exceptions

2
StopIteration
Raised when the next() method of an iterator does not point to any object.

3
SystemExit
Raised by the sys.exit() function.
4
StandardError
Base class for all built-in exceptions except StopIteration and SystemExit.

5
ArithmeticError
Base class for all errors that occur for numeric calculation.

6
OverflowError
Raised when a calculation exceeds maximum limit for a numeric type.

7
FloatingPointError
Raised when a floating point calculation fails.

8
ZeroDivisionError
Raised when division or modulo by zero takes place for all numeric types.

9
AssertionError
Raised in case of failure of the Assert statement.

10
AttributeError
Raised in case of failure of attribute reference or assignment.

11
EOFError
Raised when there is no input from either the raw_input() or input()
function and the end of file is reached.

12
ImportError
Raised when an import statement fails.

13
KeyboardInterrupt
Raised when the user interrupts program execution, usually by pressing
Ctrl+c.

14
LookupError
Base class for all lookup errors.
15
IndexError
Raised when an index is not found in a sequence.

16
KeyError
Raised when the specified key is not found in the dictionary.

17
NameError
Raised when an identifier is not found in the local or global namespace.

18
UnboundLocalError
Raised when trying to access a local variable in a function or method but
no value has been assigned to it.

19
EnvironmentError
Base class for all exceptions that occur outside the Python environment.

20
IOError
Raised when an input/ output operation fails, such as the print statement
or the open() function when trying to open a file that does not exist.

21
OSError
Raised for operating system-related errors.

22
SyntaxError
Raised when there is an error in Python syntax.

23
IndentationError
Raised when indentation is not specified properly.

24
SystemError
Raised when the interpreter finds an internal problem, but when this error
is encountered the Python interpreter does not exit.

25
SystemExit
Raised when Python interpreter is quit by using the sys.exit() function. If
not handled in the code, causes the interpreter to exit.

26
TypeError
Raised when an operation or function is attempted that is invalid for the
specified data type.

27
ValueError
Raised when the built-in function for a data type has the valid type of
arguments, but the arguments have invalid values specified.

28
RuntimeError
Raised when a generated error does not fall into any category.

29
NotImplementedError
Raised when an abstract method that needs to be implemented in an
inherited class is not actually implemented.

Python Custom Exceptions

Defining Custom Exceptions

In Python, we can define custom exceptions by creating a new class that is derived from the
built-in Exception class.
Here's the syntax to define custom exceptions,

class CustomError(Exception):
...
pass

try:
...

except CustomError:
...

Here, CustomError is a user-defined error which inherits from the Exception class.
Note:

 When we are developing a large Python program, it is a good practice to place all the user-
defined exceptions that our program raises in a separate file.

 Many standard modules define their exceptions separately


as exceptions.py or errors.py (generally but not always).

Example: Python User-Defined Exception

# define Python user-defined exceptions


class InvalidAgeException(Exception):
"Raised when the input value is less than 18"
pass

# you need to guess this number


number = 18

try:
input_num = int(input("Enter a number: "))
if input_num < number:
raise InvalidAgeException
else:
print("Eligible to Vote")

except InvalidAgeException:
print("Exception occurred: Invalid Age")
Run Code

Output
If the user input input_num is greater than 18,

Enter a number: 45
Eligible to Vote
If the user input input_num is smaller than 18,

Enter a number: 14
Exception occurred: Invalid Age

In the above example, we have defined the custom exception InvalidAgeException by


creating a new class that is derived from the built-in Exception class.
Here, when input_num is smaller than 18, this code generates an exception.
When an exception occurs, the rest of the code inside the try block is skipped.
The except block catches the user-defined InvalidAgeException exception and statements
inside the except block are executed.

Customizing Exception Classes

We can further customize this class to accept other arguments as per our needs.

To learn about customizing the Exception classes, you need to have the basic knowledge of
Object-Oriented programming.

Visit Python Object Oriented Programming to learn about Object-Oriented programming in


Python.
Let's see an example,

class SalaryNotInRangeError(Exception):
"""Exception raised for errors in the input salary.

Attributes:
salary -- input salary which caused the error
message -- explanation of the error
"""

def __init__(self, salary, message="Salary is not in (5000, 15000) range"):


self.salary = salary
self.message = message
super().__init__(self.message)

salary = int(input("Enter salary amount: "))


if not 5000 < salary < 15000:
raise SalaryNotInRangeError(salary)
Output

Enter salary amount: 2000


Traceback (most recent call last):
File "<string>", line 17, in <module>
raise SalaryNotInRangeError(salary)
__main__.SalaryNotInRangeError: Salary is not in (5000, 15000) range

Here, we have overridden the constructor of the Exception class to accept our own custom
arguments salary and message.
Then, the constructor of the parent Exception class is called manually with
the self.message argument using super().
The custom self.salary attribute is defined to be used later.
The inherited __str__ method of the Exception class is then used to display the
corresponding message when SalaryNotInRangeError is raised.

Python sys Module


The sys module in Python provides various functions and variables that are used to manipulate
different parts of the Python runtime environment. It allows operating on the interpreter as it
provides access to the variables and functions that interact strongly with the interpreter. Let’s
consider the below example.

Example:

import sys

print(sys.version)

Output:
3.6.9 (default, Oct 8 2020, 12:12:24)
[GCC 8.4.0]
In the above example, sys.version is used which returns a string containing the version of
Python Interpreter with some additional information. This shows how the sys module interacts
with the interpreter. Let us dive into the article to get more information about the sys module.
Input and Output using sys
The sys modules provide variables for better control over input or output. We can even redirect
the input and output to other devices. This can be done using three variables –

 stdin
 stdout
 stderr
stdin: It can be used to get input from the command line directly. It is used for standard input.
It internally calls the input() method. It, also, automatically adds ‘\n’ after each sentence.
Example:

import sys

for line in sys.stdin:

if 'q' == line.rstrip():

break

print(f'Input : {line}')

print("Exit")

stdout: A built-in file object that is analogous to the interpreter’s standard output stream in
Python. stdout is used to display output directly to the screen console. Output can be of any
form, it can be output from a print statement, an expression statement, and even a prompt direct
for input. By default, streams are in text mode. In fact, wherever a print function is called within
the code, it is first written to sys.stdout and then finally on to the screen.
Example:

import sys

sys.stdout.write('Geeks')

Output
Geeks

stderr: Whenever an exception occurs in Python it is written to sys.stderr.


Example:

import sys

def print_to_stderr(*a):

# Here a is the array holding the objects

# passed as the argument of the function

print(*a, file = sys.stderr)

print_to_stderr("Hello World")

Output:

Namespaces
Namespace in Python

In this tutorial, we will learn about the namespace in Python, the structure used to organize the
symbolic names assigned to objects in a Python program, why namespace is important, and
how we can use them in our Python program. Let's have a brief introduction to a namespace.

What is Namespace?

A namespace is a way of providing the unique name for each object in Python. Everything in
Python is an object, i.e., a variable or a method. In other words, it is a collection of the defined
symbolic names along with the information about the object that each name references. A
namespace can be understood as a dictionary where a name represents a key and objects are
values. Let's understand it with a real-life example - A namespace is like a surname. A "Peter"
name might be difficult to find in the class if there are multiple "Peter," but when we
particularly ask for "Peter Warner" or "Peter Cummins,". It might be rare to find the same name
and surname in a class for multiple students.

The namespace helps the Python interpreter to understand what exact method or variable is
trying to point out in the code. So its name gives more information - Name (which means name,
a unique identifier) + Space (related to scope).

In Python, there are four types of namespaces which are given below

o Built-in
o Global
o Enclosing
o Local

As these namespace various have lifetimes, Python interpreter creates namespaces as necessary
and deletes them when they are no longer needed.

Let's understand the various types of namespace in Python.

The Built-in Namespace

As its name suggests, it contains pre-defined names of all of Python's built-in objects already
available in Python. Let's list these names with the following command.

Open the Python terminal and type the following command.

Command -

1. dir(__builtins__)

Output:

['ArithmeticError', 'AssertionError', 'AttributeError', 'BaseException', 'BlockingIOError',


'BrokenPipeError', 'BufferError', 'BytesWarning', 'ChildProcessError',
'ConnectionAbortedError', 'ConnectionError', 'ConnectionRefusedError',
'ConnectionResetError', 'DeprecationWarning', 'EOFError', 'Ellipsis', 'EnvironmentError',
'Exception', 'False', 'FileExistsError', 'FileNotFoundError', 'FloatingPointError',
'FutureWarning', 'GeneratorExit', 'IOError', 'ImportError', 'ImportWarning', 'IndentationError',
'IndexError', 'InterruptedError', 'IsADirectoryError', 'KeyError', 'KeyboardInterrupt',
'LookupError', 'MemoryError', 'ModuleNotFoundError', 'NameError', 'None',
'NotADirectoryError', 'NotImplemented', 'NotImplementedError', 'OSError', 'OverflowError',
'PendingDeprecationWarning', 'PermissionError', 'ProcessLookupError', 'RecursionError',
'ReferenceError', 'ResourceWarning', 'RuntimeError', 'RuntimeWarning', 'StopAsyncIteration',
'StopIteration', 'SyntaxError', 'SyntaxWarning', 'SystemError', 'SystemExit', 'TabError',
'TimeoutError', 'True', 'TypeError', 'UnboundLocalError', 'UnicodeDecodeError',
'UnicodeEncodeError', 'UnicodeError', 'UnicodeTranslateError', 'UnicodeWarning',
'UserWarning', 'ValueError', 'Warning', 'WindowsError', 'ZeroDivisionError',
'__build_class__', '__debug__', '__doc__', '__import__', '__loader__', '__name__',
'__package__', '__spec__', 'abs', 'all', 'any', 'ascii', 'bin', 'bool', 'breakpoint', 'bytearray', 'bytes',
'callable',
'chr', 'classmethod', 'compile', 'complex', 'copyright', 'credits', 'delattr', 'dict', 'dir', 'divmod',
'enumerate', 'eval', 'exec', 'exit', 'filter', 'float', 'format', 'frozenset', 'getattr', 'globals', 'hasattr',
'hash', 'help', 'hex', 'id', 'input', 'int', 'isinstance', 'issubclass', 'iter', 'len', 'license', 'list', 'locals',
'map', 'max', 'memoryview', 'min', 'next', 'object', 'oct', 'open', 'ord', 'pow', 'print', 'property',
'quit', 'range', 'repr', 'reversed', 'round', 'set', 'setattr', 'slice', 'sorted', 'staticmethod', 'str', 'sum',
'super', 'tuple', 'type', 'vars', 'zip']

The built-in namespace creates by the Python interpreter when its starts up. These are
terminated when Python interpreter terminates.

The Global Namespace

The global namespace consists of any names in Python at any level of the main program. It is
created when the main body executes and remains in existence until the interpreter terminates.

The Python interpreter creates a global namespace for any module that our Python loads with
the import statement. To get more information, visit our Python Module.

The Local and Enclosing Namespaces

The function uses the local namespaces; the Python interpreter creates a new namespace when
the function is executed. The local namespaces remain in existence until the function
terminates. The function can also consist of another function. We can define one function inside
another as below.

Example -

1. def f():
2. print('Initiate f()')
3.
4. def g():
5. print('Initiate g()')
6. print('End g()')
7. return
8.
9. g()
10.
11. print('Initiate f()')
12. return
13. f()
In the above example, the function g() is defined within the body of f(). Inside the f() we called
the g() and called the main f() function. Let's understand the working of the above function -

o When we calls f(), Python creates a new namespace for f().


o Similarly, the f() calls g(), g() gets its own separate namespace.
o Here the g() is a local namespace created for f() is the enclosing namespace.

Each of these namespace is terminated when the function is terminated.

Scope of the Object/Variable

The scope is term which defines the coding region from a particular Python object is accessible.
Every object/variable has its scope where we can access that particular variable in the program.
For example - A variable in a function can only access inside the function. Let's understand the
following example -

Example -

1. def scope_func():
2. print("Inside scope_func")
3. def scope_inner_func():
4. var = 20
5. print("Inside inner function, value of var:",var)
6. scope_inner_func()
7. print("Try printing var from outer function: ",var)
8. scope_func()

Output:

Inside scope_func
Inside inner function, value of var: 20
Traceback (most recent call last):
File "d:/Python Project/listproblems.py", line 343, in
scope_func()
File "d:/Python Project/listproblems.py", line 342, in scope_func
print("Try printing var from outer function: ",var)
NameError: name 'var' is not defined

Python Namespace Dictionaries

Python implements global and local namespaces as dictionaries. Python comes with
the globals() and locals() methods that allow us to access global and local namespace
dictionaries.
What is the Python import module?
A file is considered as a module in python. To use the module, you have to import it
using the import keyword. The function or variables present inside the file can be used in
another file by importing the module. This functionality is available in other languages, like
typescript, JavaScript, java, ruby, etc.

How to create and import a module in Python?


Now we will create a module and import it in another file.

Here is the flow to create and import the module as shown in the screenshot:

Follow the steps given to create a module in python.

The folder structure used to test the code is as follows:

modtest/
test.py
display.py
Step 1) Create a file and name it test.py

Step 2) Inside test.py create a function called display_message()

Def display_message():
return "Welcome to Guru99 Tutorials!"
Step 3) Now create another file display.py.

Step 4) Inside display.py import the moduletest.py file, as shown below:

import test
While importing, you don’t have to mention the test.py but just the name of the file.

Step5)

Then you can call the function display_message() from test.py inside display.py, you need to
make use of module_name.function_name.

For example test.display_message().

Import test
print(test.display_message())

Step 6)

When you execute display.py, you will get the following output:

Importing a Class in Python

Earlier, we have seen a simple module with a function. Here will create a class and refer the
class inside another file.

The folder structure to test the code is as follows:

myproj/
Car.py
display.py
Create a file called Car.py with the following code:

Filename : Car.py

class Car:
brand_name = "BMW"
model = "Z4"
manu_year = "2020"

def __init__(self, brand_name, model, manu_year):


self.brand_name = brand_name
self.model = model
self.manu_year = manu_year

def car_details(self):
print("Car brand is ", self.brand_name)
print("Car model is ", self.model)
print("Car manufacture year is ", self.manu_year)

def get_Car_brand(self):
print("Car brand is ", self.brand_name)
def get_Car_model(self):
print("Car model is ", self.model)
In the file Car.py , there are attributes brand_name, model and manu_year. The functions
defined inside the class are car_details(), get_Car_brand(), get_Car_model().

Let us now use the file Car.py as a module in another file called display.py.

Filename : display.py

import Car
car_det = Car.Car("BMW","Z5", 2020)
print(car_det.brand_name)
print(car_det.car_details())
print(car_det.get_Car_brand())
print(car_det.get_Car_model())
Output:

BMW
Car brand is BMW
Car model is Z5
Car manufacture year is 2020
Car brand is BMW
Car model is Z5
So we can access all the variables and functions from Car.py using Car module.

Using from to import module


You can import only a small part of the module i.e., only the required functions and variable
names from the module instead of importing full code.

When you want only specific things to be imported, you can make use of “from” keyword to
import what you want.

So the syntax is

from module import your function_name , variables,... etc.


The folder structure used to test the code is as follows:

modtest/
test.py
display.py
In test.py there are 2 functions as shown :

Filename : test.py

defdisplay_message():
return "Welcome to Guru99 Tutorials!"

def display_message1():
return "All about Python!"
Now you want display_message() function. The function or variable that you are importing
can be directly accessed as shown below:

File Name : display.py

from test import display_message


print(display_message())

Output:

Welcome!
Now if you happen to use the function display_message1() ,it will throw an error that the
function is not defined as shown below:

from test import display_message


print(display_message1())
Output:

Traceback (most recent call last):


File "display.py", line 3, in <module>
print(display_message1())
Name Error: name 'display_message1' is not defined

Importing everything from the module


Import allows you to import the full module by using import followed by module name, i.e.,
the filename or the library to be used.

Syntax:

Import module
Or by using

from module import *


The folder structure used to test the code is as follows:

modtest/
test.py
display.py
Following are the code details inside test.py

my_name = "Python"
my_address = "Mumbai"

defdisplay_message():
return "Welcome!"
def display_message1():
return "All about Python!"

Using the import module


Using just import module name, to refer to the variables and functions inside the module, it
has to prefix with module name.

Example

Filename : display.py

Import test
print(test.display_message())
print(test.display_message1())
print(test.my_name)
print(test.my_address)
The module name test is used to refer to the function and variables inside module test.

Output:

Teacher!
All about Python!
Python
Mumbai

Using import *
Let us see an example using import *. Using import *, the functions and variables are
directly accessible, as shown in the example below:

from test import *

print(display_message())
print(display_message1())
print(my_name)
print(my_address)
Output:

Teacher!
All about Python!
Python
Mumbai
A package is a container that contains various functions to perform specific tasks. For
example, the math package includes the sqrt() function to perform the square root of a
number.
While working on big projects, we have to deal with a large amount of code, and writing
everything together in the same file will make our code look messy. Instead, we can separate
our code into multiple files by keeping the related code together in packages.

Now, we can use the package whenever we need it in our projects. This way we can also
reuse our code.

Package Model Structure in Python Programming

Suppose we are developing a game. One possible organization of packages and modules
could be as shown in the figure below.

Game Package Model Structure


Note: A directory must contain a file named __init__.py in order for Python to consider it as
a package. This file can be left empty but we generally place the initialization code for that
package in this file.

Importing module from a package

In Python, we can import modules from packages using the dot (.) operator.

For example, if we want to import the start module in the above example, it can be done as
follows:

import Game.Level.start

Now, if this module contains a function named select_difficulty(), we must use the full name
to reference it.

Game.Level.start.select_difficulty(2)

Import Without Package Prefix

If this construct seems lengthy, we can import the module without the package prefix as
follows:

from Game.Level import start

We can now call the function simply as follows:

start.select_difficulty(2)
Import Required Functionality Only

Another way of importing just the required function (or class or variable) from a module
within a package would be as follows:

from Game.Level.start import select_difficulty

Now we can directly call this function.

select_difficulty(2)

Although easier, this method is not recommended. Using the full namespace avoids confusion
and prevents two same identifier names from colliding.
While importing packages, Python looks in the list of directories defined in sys.path, similar
as for module search path.

You might also like