0% found this document useful (0 votes)
2 views24 pages

2.II-FIles_Exceptions & Modules Part-II 2022-23

The document provides lecture notes on Python programming, specifically focusing on file handling techniques. It covers various aspects such as opening, reading, writing, and closing files, along with different access modes and the use of the 'with' statement for file operations. Additionally, it discusses the Python os module for file processing operations like renaming and deleting files.
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)
2 views24 pages

2.II-FIles_Exceptions & Modules Part-II 2022-23

The document provides lecture notes on Python programming, specifically focusing on file handling techniques. It covers various aspects such as opening, reading, writing, and closing files, along with different access modes and the use of the 'with' statement for file operations. Additionally, it discusses the Python os module for file processing operations like renaming and deleting files.
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/ 24

II B.

Tech I- Semester Python Programming (2022-23)

LECTURE NOTES

ON

PYTHON PROGRAMMING

(AI & ML)

II B. Tech I semester [2022-23]

R18 Regulation

Dr. N.Venkateswaran,
Associate Professor,

Department of Computer Science & Engineering

Prepared by Dr. N.Venkateswaran, Associate Professor, CSE Dept, JITS Page 1


PYTHON PROGRAMMING –UNIT-II
Python File Handling
Till now, we were taking the input from the console and writing it back to the console to interact with the
user.
Sometimes, it is not enough to only display the data on the console. The data to be displayed may be
very large, and only a limited amount of data can be displayed on the console, and since the memory is
volatile, it is impossible to recover the programmatically generated data again and again.
However, if we need to do so, we may store it onto the local file system which is volatile and can be
accessed every time. Here, comes the need of file handling.
We will learn all about file handling in python including,
 creating a file,
 opening a file,
 closing a file,
 Reading, Writing and appending the file, etc.

Opening a file
Python provides the open() function which accepts two arguments, file name and access mode in which the
file is accessed. The function returns a file object which can be used to perform various operations like
reading, writing, etc.
The syntax to use the open() function is given below.

file object = open(<file-name>, <access-mode>, <buffering>)


The files can be accessed using various modes like read, write, or append. The following are the details
about the access mode to open a file.
Access
SN Description
mode
It opens the file to read-only. The file pointer exists at the beginning. The file is by default
1 r
open in this mode if no access mode is passed.
It opens the file to read only in binary format. The file pointer exists at the beginning of the
2 rb
file.
3 r+ It opens the file to read and write both. The file pointer exists at the beginning of the file.
It opens the file to read and write both in binary format. The file pointer exists at the
4 rb+
beginning of the file.
It opens the file to write only. It overwrites the file if previously exists or creates a new one if
5 w
no file exists with the same name. The file pointer exists at the beginning of the file.
It opens the file to write only in binary format. It overwrites the file if it exists previously or
6 wb creates a new one if no file exists with the same name. The file pointer exists at the beginning
of the file.
It opens the file to write and read both. It is different from r+ in the sense that it overwrites
7 w+ the previous file if one exists whereas r+ doesn't overwrite the previously written file. It
creates a new file if no file exists. The file pointer exists at the beginning of the file.
It opens the file to write and read both in binary format. The file pointer exists at the
8 wb+
beginning of the file.

Prepared by Dr. N.Venkateswaran, Associate Professor, CSE Dept, JITS Page 2


It opens the file in the append mode. The file pointer exists at the end of the previously
9 a
written file if exists any. It creates a new file if no file exists with the same name.
It opens the file in the append mode in binary format. The pointer exists at the end of the
10 Ab previously written file. It creates a new file in binary format if no file exists with the same
name.
It opens a file to append and read both. The file pointer remains at the end of the file if a file
11 a+
exists. It creates a new file if no file exists with the same name.
It opens a file to append and read both in binary format. The file pointer remains at the end
12 ab+
of the file.
It opens a file in exclusive mode, only for write operation. It causes an error a file exists with
13 X
the same name..
It opens a file in exclusive mode, only for binary write operation. It causes an error a file exists
14 xb
with the same name..

Let's look at the simple example to open a file named "file.txt" (stored in the same directory) in read mode
and printing its content on the console.

Example
#opens the file file.txt in read mode
fileptr = open("file.txt","r")

if fileptr:
print("file is opened successfully")

Output:
<class '_io.TextIOWrapper'>
file is opened successfully

The close() method


Once all the operations are done on the file, we must close it through our python script using the close()
method. Any unwritten information gets destroyed once the close() method is called on a file object.
We can perform any operation on the file externally in the file system is the file is opened in python, hence it
is good practice to close the file once all the operations are done.

The syntax to use the close() method is given below.

fileobject.close()
Consider the following example.
Example
# opens the file file.txt in read mode fileptr =
open("file.txt","r")

if fileptr:
print("file is opened successfully")

#closes the opened file


fileptr.close()

Prepared by Dr. N.Venkateswaran, Associate Professor, CSE Dept, JITS Page 3


Reading the file
To read a file using the python script, the python provides us the read() method. The read() method reads a
string from the file. It can read the data in the text as well as binary format.

The syntax of the read() method is given below.

fileobj.read(<count>)
Here, the count is the number of bytes to be read from the file starting from the beginning of the file. If the
count is not specified, then it may read the content of the file until the end.
Consider the following example.
Example
#open the file.txt in read mode. Causes error if no such file exists.
fileptr = open("file.txt","r");

#stores all the data of the file into the variable content
content = fileptr.read(9);

# prints the type of the data stored in the file


print(type(content))

#prints the content of the file


print(content)

#closes the opened file


fileptr.close()

Output:
<class 'str'>Hi, I am

Read Lines of the file


Python facilitates us to read the file line by line by using a function readline(). The readline() method reads
the lines of the file from the beginning, i.e., if we use the readline() method two times, then we can get the
first two lines of the file.
Consider the following example which contains a function readline() that reads the first line of our file
"file.txt" containing three lines.

Example
#open the file.txt in read mode. causes error if no such file exists.
fileptr = open("file.txt","r");

#stores all the data of the file into the variable content
content = fileptr.readline();

Prepared by Dr. N.Venkateswaran, Associate Professor, CSE Dept, JITS Page 4


# prints the type of the data stored in the file
print(type(content))

#prints the content of the file


print(content)

#closes the opened file

fileptr.close()

Output:
<class 'str'>
Hi, I am the file and being used as

Looping through the file


By looping through the lines of the file, we can read the whole file.
Example
#open the file.txt in read mode. causes an error if no such file
exists. fileptr = open("file.txt","r");

#running a
for loopfor i
in fileptr:
print(i) # i contains each line of the file
Output:
Hi, I am the file and being used as an
example to read a file in python.

Writing the file


To write some text to a file, we need to open the file using the open method with one of the following access
modes.
a: It will append the existing file. The file pointer is at the end of the file. It creates a new file if no
file exists.
w: It will overwrite the file if any file exists. The file pointer is at the beginning of the file. Consider the
following example.

Example 1
#open the file.txt in append mode. Creates a new file if no such file exists.
fileptr = open("file.txt","a");

#appending the content to the file


fileptr.write("Python is the modern day language. It makes things so simple.")

#closing the opened file


fileptr.close();
Now, we can see that the content of the file is modified.
Prepared by Dr. N.Venkateswaran, Associate Professor, CSE Dept, JITS Page 5
File.txt:
Hi, I am the file and being used asan
example to read a
file in python.
Python is the modern day language. It makes things so simple.
Example 2
#open the file.txt in write mode.
fileptr = open("file.txt","w");
#overwriting the content of the file
fileptr.write("Python is the modern day language. It makes things so simple.")

#closing the opened file


fileptr.close();
Now, we can check that all the previously written content of the file is overwritten with the new text we
have passed.
File.txt:
Python is the modern day language. It makes things so simple.

Creating a new file


The new file can be created by using one of the following access modes with the function open().
x: it creates a new file with the specified name. It causes an error a file exists with the same name.
a: It creates a new file with the specified name if no such file exists. It appends the content to the file if
the file already exists with the specified name.
w: It creates a new file with the specified name if no such file exists. It overwrites the existing file.
Consider the following example.
Example
#open the file.txt in read mode. causes error if no such file exists. fileptr =
open("file2.txt","x");

print(fileptr)if fileptr:
print("File created successfully");

Output:
File created successfully

Prepared by Dr. N.Venkateswaran, Associate Professor, CSE Dept, JITS Page 6


Using ‘with’ statement with files
The ‘with’ statement was introduced in python 2.5. It is useful in the case of manipulating the files. It is
used in the scenario where a pair of statements is to be executed with a block of code in between.
The syntax to open a file using with statement is given below.
with open(<file name>, <access mode>) as <file-pointer>:
#statement suite
The advantage of using with statement is that it provides the guarantee to close the file regardless of how
thenested block exits.
It is always suggestible to use the with statement in the case of file s because, if the break, return, or
exception occurs in the nested block of code then it automatically closes the file. It doesn't let the file to be
corrupted.
Consider the following example.
Example
with open("file.txt",'r') as
f:content = f.read();
print(content)
Output:
Python is the modern day language. It makes things so simple.

File Pointer positions


Python provides the tell() method which is used to print the byte number at which the file pointer exists.
Consider the following example.
Example
# open the file file2.txt in read mode
fileptr = open("file2.txt","r")

#initially the filepointer is at 0


print("The filepointer is at byte :",fileptr.tell())

#reading the content of the file


content = fileptr.read();

#after the read operation file pointer modifies. tell() returns the location of the fileptr.

print("After reading, the filepointer is at:",fileptr.tell())


Output:
The filepointer is at byte : 0
After reading, the filepointer is at 26

Modifying file pointer position


In the real world applications, sometimes we need to change the file pointer location externally since
we may need to read or write the content at various locations.
For this purpose, the python provides us the seek() method which enables us to modify the file pointer
position externally.

Prepared by Dr. N.Venkateswaran, Associate Professor, CSE Dept, JITS Page 1


The syntax to use the seek() method is given below.
<file-ptr>.seek(offset[, from)
The seek() method accepts two parameters:
offset: It refers to the new position of the file pointer within the file.
from: It indicates the reference position from where the bytes are to be moved. If it is set to 0, the
beginning of the file is used as the reference position. If it is set to 1, the current position of the
file pointer is used as the reference position. If it is set to 2, the end of the file pointer is used as the
reference position.
Consider the following example.
Example
# open the file file2.txt in read mode
fileptr = open("file2.txt","r")

#initially the filepointer is at 0


print("The filepointer is at byte :",fileptr.tell())

#changing the file pointer location to 10.


fileptr.seek(10);

#tell() returns the location of the fileptr.


print("After reading, the filepointer is at:",fileptr.tell())

Output:
The filepointer is at byte : 0
After reading, the filepointer is at 10

Python os module
The os module provides us the functions that are involved in file processing operations like renaming,
deleting, etc.
Let's look at some of the os module functions.

Renaming the file


The os module provides us the rename() method which is used to rename the specified file to a new name.
The syntax to use the rename() method is given below.

rename(?current-name?, ?new-name?)
Example
import os;

#rename file2.txt to file3.txt


os.rename("file2.txt","file3.txt")

Removing the file


The os module provides us the remove() method which is used to remove the specified file. The syntax
to use the remove() method is given below.
remove(?file-name?)
Prepared by Dr. N.Venkateswaran, Associate Professor, CSE Dept, JITS Page 2
Example
import os;
#deleting the file named file3.txt
os.remove("file3.txt")

Creating the new directory


The mkdir() method is used to create the directories in the current working directory. The syntax to create
the new directory is given below.
mkdir(?directory name?)
Example
import os;

#creating a new directory with the name new


os.mkdir("new")

Changing the current working directory


The chdir() method is used to change the current working directory to a specified directory.
The syntax to use the chdir() method is given below.
chdir("new-directory")
Example
import os;
#changing the current working directory to new
os.chdir("new")
The getcwd() method
This method returns the current working
directory. The syntax to use the getcwd() method is
given below.
os.getcwd()
Example
import os;

#printing the current working directory


print(os.getcwd())

Deleting directory
The rmdir() method is used to delete the specified directory. The syntax to use the rmdir() method is given
below.os.rmdir(?directory name?)
Example
import os;

#removing the new directory


os.rmdir("new")

Prepared by Dr. N.Venkateswaran, Associate Professor, CSE Dept, JITS Page 3


Writing python output to the files
In python, there are the requirements to write the output of a python script to a file.
The check_call() method of module subprocess is used to execute a python script and write the output of
that script to a file.
The following example contains two python scripts. The script file1.py executes the script file.py
andwrites its output to the text file output.txt
file.py:
temperatures=[10,-20,-289,100]def
c_to_f(c):
if c< -273.15:
return "That temperature doesn't make
sense!"else:
f=c*9/5+3
2return f
for t in
temperatures:
print(c_to_f(t))
file1.py:
import subprocess

with open("output.txt", "wb") as f:


subprocess.check_call(["python", "file.py"],
stdout=f)
Output:
50
-4
That temperature doesn't make
sense!212

The file related methods


The file object provides the following methods to manipulate the files on various operating systems.
SN Method Description
It closes the opened file. The file once closed, it can't be read or write any
1 file.close()
more.
2 File.fush() It flushes the internal buffer.
It returns the file descriptor used by the underlying implementation to request
3 File.fileno()
I/O from the OS.
4 File.isatty() It returns true if the file is connected to a TTY device, otherwise returns false.
5 File.next() It returns the next line from the file.
6 File.read([size]) It reads the file for the specified size.
It reads one line from the file and places the file pointer to the beginning of
7 File.readline([size])
the new line.
It returns a list containing all the lines of the file. It reads the file until the
8 File.readlines([sizehint])
EOF occurs using readline() function.
It modifies the position of the file pointer to a specified offset with the
9 File.seek(offset[,from)
specified reference.

Prepared by Dr. N.Venkateswaran, Associate Professor, CSE Dept, JITS Page 4


10 File.tell() It returns the current position of the file pointer within the file.
11 File.truncate([size]) It truncates the file to the optional specified size.
12 File.write(str) It writes the specified string to a file
13 File.writelines(seq) It writes a sequence of the strings to a file.

Prepared by Dr. N.Venkateswaran, Associate Professor, CSE Dept, JITS Page 5


Python Exceptions
An exception can be defined as an abnormal condition in a program resulting in the disruption in the flow of
the program.
Whenever an exception occurs, the program halts the execution, and thus the further code is not executed.
Therefore, an exception is the error which python script is unable to tackle with.

Python provides us with the way to handle the Exception so that the other part of the code can be executed
without any disruption. However, if we do not handle the exception, the interpreter doesn't execute all the
code that exists after the that.

Common Exceptions
A list of common exceptions that can be thrown from a normal python program is given below.
1. ZeroDivisionError: Occurs when a number is divided by zero.
2. NameError: It occurs when a name is not found. It may be local or global.
3. IndentationError: If incorrect indentation is given.
4. IOError: It occurs when Input Output operation fails.
5. EOFError: It occurs when the end of the file is reached, and yet operations are being performed.

Problem without handling exceptions


As we have already discussed, the exception is an abnormal condition that halts the execution of the
program. Consider the following example.

Example
a = int(input("Enter a:"))
b = int(input("Enter b:"))
c = a/b;
print("a/b = %d"%c)

#other code:
print("Hi I am other part of the program")

Output:
Enter a:10
Enter b:0
Traceback (most recent call last):
File "exception-test.py", line 3, in <module>
c = a/b;
ZeroDivisionError: division by zero

Exception handling in python


If the python program contains suspicious code that may throw the exception, we must place that code in the
try block. The try block must be followed with the except statement which contains a block of code that will
be executed if there is some exception in the try block.

Prepared by Dr. N.Venkateswaran, Associate Professor, CSE Dept, JITS Page 6


Syntax

try:
#block of code
except Exception1:
#block of code
except Exception2:
#block of code

#other code

We can also use the else statement with the try-except statement in which, we can place the code which will
be executed in the scenario if no exception occurs in the try block.

The syntax to use the else statement with the try-except statement is given below.
try:
#block of code

except Exception1:
#block of code

else:
#this code executes if no except block is executed

Prepared by Dr. N.Venkateswaran, Associate Professor, CSE Dept, JITS Page 7


Example
try:
a = int(input("Enter a:"))
b = int(input("Enter b:"))
c = a/b;
print("a/b = %d"%c)
except Exception:
print("can't divide by zero")
else:
print("Hi I am else block")

Output:
Enter a:10
Enter b:2
a/b = 5
Hi I am else block

The except statement with no exception


Python provides the flexibility not to specify the name of exception with the except statement.
Consider the following example.

Example
try:
a = int(input("Enter a:"))
b = int(input("Enter b:"))
c = a/b;
print("a/b = %d"%c)
except:
print("can't divide by zero")
else:
print("Hi I am else block")

Prepared by Dr. N.Venkateswaran, Associate Professor, CSE Dept, JITS Page 8


Output:
Enter a:10
Enter b:0
can't divide by zero

Points to remember
1. Python facilitates us to not specify the exception with the except statement.
2. We can declare multiple exceptions in the except statement since the try block may contain the
statements which throw the different type of exceptions.
3. We can also specify an else block along with the try-except statement which will be executed if no
exception is raised in the try block.
4. The statements that don't throw the exception should be placed inside the else block.

Example
try:
#this will throw an exception if the file doesn't exist.
fileptr = open("file.txt","r")
except IOError:
print("File not found")
else:
print("The file opened successfully")
fileptr.close()

Output:
File not found
Declaring multiple exceptions
The python allows us to declare the multiple exceptions with the except clause. Declaring multiple
exceptions is useful in the cases where a try block throws multiple exceptions.

Syntax
try:
#block of code
except (<Exception 1>,<Exception 2>,<Exception 3>,...<Exception n>)
#block of code
else:
#block of code

Example
try:
a=10/0;
except ArithmeticError,StandardError:
print "Arithmetic Exception"
else:
print "Successfully Done"

Output:
Prepared by Dr. N.Venkateswaran, Associate Professor, CSE Dept, JITS Page 9
Arithmetic Exception

The finally block


We can use the finally block with the try block in which, we can pace the important code which must be
executed before the try statement throws an exception.
The syntax to use the finally block is given below.
Syntax
try:
# block of code
# this may throw an exception
finally:
# block of code
# this will always be executed

Example
try:
fileptr = open("file.txt","r") try:
fileptr.write("Hi I am good") finally:
fileptr.close() print("file closed")
except:
print("Error")

Output:
file closed Error

Raising exceptions
An exception can be raised by using the raise clause in python. The syntax to use the raise statement is given

Prepared by Dr. N.Venkateswaran, Associate Professor, CSE Dept, JITS Page 10


below.
Syntax
raise Exception_class,<value>
Points to remember
1. To raise an exception, raise statement is used. The exception class name follows it.
2. An exception can be provided with a value that can be given in the parenthesis.
3. To access the value "as" keyword is used. "e" is used as a reference variable which stores the value
of the exception.

Examp
le
try:
age = int(input("Enter the age?"))
if age<18:
raise ValueError;
else:
print("the age is valid")
except ValueError:
print("The age is not valid")

Output:
Enter the age?17
The age is not valid
Example
try:
a = int(input("Enter a?"))
b = int(input("Enter b?"))
if b is 0:
raise ArithmeticError
else:
print("a/b = ",a/b)
except ArithmeticError:
print("The value of b can't be 0")
Output:
Enter a?10Enter b?0
The value of b can't be 0

Custom Exception
The python allows us to create our exceptions that can be raised from the program and caught using the
except clause. However, we suggest you read this section after visiting the Python object and classes.

Consider the following example.


Example
class ErrorInCode(Exception):
def init (self, data):
self.data = data
def str (self):
return repr(self.data)

try:
Prepared by Dr. N.Venkateswaran, Associate Professor, CSE Dept, JITS Page 11
raise ErrorInCode(2000)
except ErrorInCode as ae:
print("Received error:", ae.data)
Output:
Received error: 2000

Python Assertion
Python assert keyword is defined as a debugging tool that tests a condition. The Assertions are mainly the
assumption that asserts or state a fact confidently in the program. For example, while writing a division
function, the divisor should not be zero, and you assert that divisor is not equal to zero.
It is simply a boolean expression that has a condition or expression checks if the condition returns true
or false. If it is true, the program does not do anything, and it moves to the next line of code. But if it is false,
it raises an AssertionError exception with an optional error message.

The main task of assertions is to inform the developers about unrecoverable errors in the program like "file
not found", and it is right to say that assertions are internal self-checks for the program. They work by
declaring some conditions as impossible in your code. If one of the conditions does not hold, that means there
is a bug in the program.

Why Assertion is used


It is a debugging tool, and its primary task is to check the condition. If it finds that condition is true, it moves
to the next line of code, and If not then stops all its operations and throws an error. It points out the error in
the code.

Where Assertion in Python used


 Checking the outputs of the functions.
 Used for testing the code.
 In checking the values of arguments.
 Checking the valid input.

Syntax
assert condition, error_message(optional)

Example1
This example shows the working of assert with the error message.
def avg(scores):
assert len(scores) != 0,"The List is empty."
return sum(scores)/len(scores)

scores2 = [67,59,86,75,92]
print("The Average of scores2:",avg(scores2))

scores1 = []
print("The Average of scores1:",avg(scores1))

Prepared by Dr. N.Venkateswaran, Associate Professor, CSE Dept, JITS Page 12


Output:
The Average of scores2: 75.8
AssertionError: The List is empty.

Explanation: In the above example, we have passed a non-empty list scores2 and an empty list scores1 to
the avg() function. We received an output for scores2 list successfully, but after that, we got an error

AssertionError: List is empty.The assert condition is satisfied by the scores2 list and lets the program to
continue to run. However, scores1 doesn't satisfy the condition and gives an AssertionError.

Example2:
This example shows the "Divide by 0 error" in the console.
# initializing number
x=7
y=0
# It uses assert to check for 0
print ("x / y value is : ")
assert y != 0, "Divide by 0 error"
print (x / y)
Output:
x / y value is :
Runtime Exception :
Traceback (most recent call last):
File "main.py", line 6, in <module>
assert y != 0, "Divide by 0 error"
AssertionError: Divide by 0 error

Explanation: In the above example, we initialize an integer variable, i.e., x=7, y=0, and try to print the
value of x/y as an output. The compiler generates a Runtime Exception because of the assert keyword which
displays "Divide by 0 error" in the console.

Points to remember: Python - Assert Statement


Python provides the assert statement to check if a given logical expression is true or false. Program
execution proceeds only if the expression is true and raises the AssertionError when it is false. The
following code shows the usage of the assert statement.
Example: assert
num=int(input('Enter a number: '))
assert num>=0
print('You entered: ', num)

The print statement will display if the entered number is greater than or equal to 0. Negative numbers result
in aborting the program after showing the AssertionError.

Result:
Enter a number: 100
You entered 100

Prepared by Dr. N.Venkateswaran, Associate Professor, CSE Dept, JITS Page 13


Enter a number: -10
Traceback (most recent call last):
File "C:/python36/xyz.py", line 2, in <module>
assert num>=0
AssertionError

The assert statement can optionally include an error message string, which gets displayed along with
the AssertionError. In the above code, you can change the assert statement to the following and run the code:

Example: assert
num=int(input('Enter a number: '))
assert num>=0, "Only positive numbers accepted."
print('You entered: ', num)

Result:
Enter a number: -10
Traceback (most recent call last):
File "C:/python36/xyz.py", line 2, in <module> assert
num>=0, "Only positive numbers accepted."AssertionError:
Only positive numbers accepted.

Python Modules
A python module can be defined as a python program file which contains a python code including python
functions, class, or variables. In other words, we can say that our python code file saved with the extension
(.py) is treated as the module. We may have a runnable code inside the python module.
Modules in Python provides us the flexibility to organize the code in a logical way.
To use the functionality of one module into another, we must have to import the specific module.
Example
In this example, we will create a module named as file.py which contains a function func that contains a
code to print some message on the console.
Let's create the module named as file.py.
#displayMsg prints a message to the name being passed.
def displayMsg(name) :
print("Hi "+name);

Here, we need to include this module into our main module to call the method displayMsg() defined in the
module named file.

Loading the module in our python code


We need to load the module in our python code to use its functionality. Python provides two types of
statements as defined below.
1. The import statement
2. The from-import statement

Prepared by Dr. N.Venkateswaran, Associate Professor, CSE Dept, JITS Page 14


The import statement
The import statement is used to import all the functionality of one module into another. Here, we must
notice that we can use the functionality of any python source file by importing that file as the module into
another python source file.

We can import multiple modules with a single import statement, but a module is loaded once regardless of
the number of times, it has been imported into our file.

The syntax to use the import statement is given below.


import module1, module2,. module n
Hence, if we need to call the function displayMsg() defined in the file file.py, we have to import that file as a
module into our module as shown in the example below.

Example:
import file
name = input("Enter the name?")
file.displayMsg(name)

Output:
Enter the name?
John Hi John

The from-import statement


Instead of importing the whole module into the namespace, python provides the flexibility to import only the
specific attributes of a module. This can be done by using from? import statement.

The syntax to use the from-import statement is given below.


from < module-name> import <name 1>, <name
2>..,<name n>
Consider the following module named as calculation which contains three functions as summation,
multiplication, and divide.
calculation.py:
#place the code in the calculation.py
def summation(a,b):
return a+b
def multiplication(a,b):
return a*b;
def divide(a,b):
return a/b;
Main.py:
from calculation import summation
#it will import only the summation() from calculation.py
a = int(input("Enter the first number"))
Prepared by Dr. N.Venkateswaran, Associate Professor, CSE Dept, JITS Page 15
b = int(input("Enter the second number"))
print("Sum = ",summation(a,b)) #we do not need to specify the module name while accessing summ
ation()

Output:

Enter the first number10


Enter the second number20
Sum = 30

The from...import statement is always better to use if we know the attributes to be imported from the module
in advance. It doesn't let our code to be heavier. We can also import all the attributes from a module by
using *.

Consider the following syntax.


from <module> import *

Renaming a module
Python provides us the flexibility to import some module with a specific name so that we can use this name
to use that module in our python source file.

The syntax to rename a module is given below.


import <module-name> as <specific-name>
Example
#the module calculation of previous example is imported in this example as cal.

import calculation as cal


a = int(input("Enter a?"))
b = int(input("Enter b?"))
print("Sum = ",cal.summation(a,b))
Output:
Enter a?10
Enter b?20
Sum = 30

Using dir() function


The dir() function returns a sorted list of names defined in the passed module. This list contains all the sub-
modules, variables and functions defined in this module.
Consider the following example.

Example
1. import json
2.
3. List = dir(json)

Prepared by Dr. N.Venkateswaran, Associate Professor, CSE Dept, JITS Page 16


4.
5. print(List)

Output:
['JSONDecoder', 'JSONEncoder', ' all ', ' author ', ' builtins ',
' cached ', ' doc ',
' file ', ' loader ', ' name ', ' package ', ' path ',
' spec ', ' version ',
'_default_decoder', '_default_encoder', 'decoder', 'dump', 'dumps',
'encoder', 'load', 'loads', 'scanner']

The reload() function


As we have already stated that, a module is loaded once regardless of the number of times it is imported into
the python source file. However, if you want to reload the already imported module to re-execute the top-
level code, python provides us the reload() function.
The syntax to use the reload() function is given below.
reload(<module-name>)
for example, to reload the module calculation defined in the previous example, we must use the following
line of code.
reload(calculation)

Scope of variables
In Python, variables are associated with two types of scopes. All the variables defined in a module contain
the global scope unless or until it is defined within a function.

All the variables defined inside a function contain a local scope that is limited to this function itself. We can
not access a local variable globally.

If two variables are defined with the same name with the two different scopes, i.e., local and global, then the
priority will always be given to the local variable.

Consider the following example.


Example
name = "john"
def print_name(name):
print("Hi",name) #prints the name that is local to this function only.
name = input("Enter the name?")
print_name(name)

Output:
Hi David

Prepared by Dr. N.Venkateswaran, Associate Professor, CSE Dept, JITS Page 17


Python packages
The packages in python facilitate the developer with the application development environment by providing
a hierarchical directory structure where a package contains sub-packages, modules, and sub-modules. The
packages are used to categorize the application level code efficiently.

Let's create a package named Employees in your home directory. Consider the following steps.
1. Create a directory with name Employees on path /home.
2. Create a python source file with name ITEmployees.py on the path /home/Employees.

ITEmployees.py
1. def getITNames():
2. List = ["Varun", "Joel", "Shailu", "Snigdha"]
3. return List;

3. Similarly, create one more python file with name BPOEmployees.py and create a function
getBPONames().

4. Now, the directory Employees which we have created in the first step contains two python modules. To
make this directory a package, we need to include one more file here, that is init .py which contains the
import statements of the modules defined in this directory.

init .py
1. from ITEmployees import getITNames
2. from BPOEmployees import getBPONames

5. Now, the directory Employees has become the package containing two python modules. Here we must
notice that we must have to create init .py inside a directory to convert this directory to a package.

6. To use the modules defined inside the package Employees, we must have to import this in our python
source file. Let's create a simple python source file at our home directory (/home) which uses the modules
defined in this package.

Test.py
import Employees
print(Employees.getNames())

Output:
[‘Varun’, ‘Joel’, ‘Shailu’, ’Snigdha’]

Prepared by Dr. N.Venkateswaran, Associate Professor, CSE Dept, JITS Page 18

You might also like