[go: up one dir, main page]

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

Python 214 275

PYTHON HANDBOOK

Uploaded by

Shalom Salve
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 views62 pages

Python 214 275

PYTHON HANDBOOK

Uploaded by

Shalom Salve
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/ 62

CHAPTER

Modules, Working with Files and


Exception Handling
Objectives…
To learn Concepts of Files
To study Basic Concepts in Modules
To understand Packages
To learn Regular Expressions
To study Exception Handling

4.0 INTRODUCTION
• Modules and packages are the constructs in Python programming that promote code
modularization.
• The modularization (modular programming) refers to the process of breaking a large
programming task into separate, smaller, more manageable subtasks or modules.
• A module in Python programming allows us to logically organize the python code. A
module is a single source code file.
• The module in Python has the .py file extension. The name of the module will be the
name of the file.
• 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.
• In Python, packages allow us to create a hierarchical file directory structure of
modules. For example, mymodule.mod1 stands for a module mod1, in the package
mymodule.
• A Python package is a collection of modules which have a common purpose. In short,
modules are grouped together to forms packages.
• A file is a collection of related data that acts as a container of storage as data
permanently.
• The file processing refers to a process in which a program processes and accesses data
stored in files.

4.1
Python Programming Modules, Working with Files and Exception Handling

• A file is a computer resource used for recording data in a computer storage device.
The processing on a file is performed using read/write operations performed by
programs.
• Python supports file handling and allows users to handle files i.e., to read and write
files, along with many other file handling options, to operate on files.
• Python programming provides modules with functions that enable us to manipulate
text files and binary files. Python allows us to create files, update their contents and
also delete files.
• Files are named locations on disk to store related information. They are used to
permanently store data in a non-volatile memory (e.g. hard disk).
• A text file is a file that stores information in the term of a sequence of characters
(textual information), while a binary file stores data in the form of bits (0s and 1s) and
used to store information in the form of text, images, audios, videos etc.
• The file handling plays an important role when the data needs to be stored
permanently into the file. A file is a named location on disk to store related
information.
• We can access the stored information (non-volatile) after the program termination.
Python has several functions for creating, reading, updating, and deleting files.
• When an error occurs, or exception as we call it, Python will normally stop and
generate an error message. Exception in Python is nothing but errors which are
encountered at the run time.
• Exception Handling is the mechanism it is allows to handle errors very smartly while
the program is running.

4.1 MODULES
• Modules are primarily the (.py) files which contain Python programming code
defining functions, class, variables, etc. with a suffix .py appended in its file name. A
file containing .py python code is called a module.
• If we want to write a longer program, we can use file where we can do editing,
correction. This is known as creating a script. As the program gets longer, we may
want to split it into several files for easier maintenance.
• We may also want to use a function that we have written in several programs without
copying its definition into each program.
• In Python we can put definitions in a file and use them in a script or in an interactive
instance of the interpreter. Such a file is called a module.
4.1.1 Writing/Creating and Exploring Module
• Writing a module means simply creating a file which can contains python definitions
and statements. The file name is the module name with the extension .py. To include
module in a file, use ‘import’ statement.
• Follow the following steps to create modules:
1. Create a first file as a python program with extension as .py. This is your module
file where we can write a function which performs some task.
4.2
Python Programming Modules, Working with Files and Exception Handling

2. Create a second file in the same directory called main file where we can import the
module to the top of the file and call the function.
• Second file needs to be in the same directory so that Python knows where to find the
module since it’s not a built-in module.

Example: For creating a module. Type the following code and save it as p1.py.
def add(a, b):
"This function adds two numbers and return the result"
result = a + b
return result
def sub(a, b):
"This function subtract two numbers and return the result"
result = a - b
return result
def mul(a, b):
"This function multiply two numbers and return the result"
result = a * b
return result
def div(a, b):
"This function divide two numbers and return the result"
result = a / b
return result
Import the definitions inside a module:
import p1
print("Addition=" , p1.add(10,20))
print("Subtraction=" ,p1.sub(10,20))
print("Multiplication=" ,p1.mul(10,20))
print("division=" ,p1.div(10,20))
Output:
Addition= 30
Subtraction= -10
Multiplication= 200
division= 0.5
Exploring Modules:
• Modules in Python are simply Python files with a .py extension. The name of the
module will be the name of the file. A Python module can have a set of functions,
classes or variables defined and implemented.
• To exploring modules in Python the dir() function is used. For example, we have
defined a function add() in the module example that we had in the beginning.
4.3
Python Programming Modules, Working with Files and Exception Handling

• We can use dir in example module in the following way:


>>> dir(example)
['__builtins__',
'__cached__',
'__doc__',
'__file__',
'__initializing__',
'__loader__',
'__name__',
'__package__',
'add']
• Here, we can see a sorted list of names (along with add). All other names that begin
with an underscore are default Python attributes associated with the module (not
user-defined).
• Python gives us several different ways to view module content. The method that most
developers use is to work with the dir() function, which tells us about the attributes
that the module provides.
• Function attributes are automatically generated by Python for you. These attributes
perform the following tasks or contain the following information:
1. __builtins__: Contains a listing of all the built-in attributes that are accessible from
the module. Python adds these attributes automatically for us.
2. __cached__: Tells us the name and location of the cached file that is associated with
the module. The location information (path) is relative to the current Python
directory.
3. __doc__: Outputs help information for the module, assuming that you’ve actually
filled it in. For example, if we type os.__doc__ and press Enter, Python will output
the help information associated with the os library.
4. __file__: Tells us the name and location of the module. The location information
(path) is relative to the current Python directory.
5. __initializing__: Determines whether the module is in the process of initializing
itself. Normally this attribute returns a value of False. This attribute is useful when
we need to wait until one module is done loading before we import another
module that depends on it.
6. __loader__: Outputs the loader information for this module. The loader is a piece
of software that gets the module and puts it into memory so that Python can use it.
This is one attribute you rarely (if ever) use.
7. __name__: Tells us just the name of the module.
8. __package__: This attribute is used internally by the import system to make it
easier to load and manage modules. We don’t need to worry about this particular
attribute.
4.4
Python Programming Modules, Working with Files and Exception Handling

4.1.2 Importing Modules


• The import statement is used to imports a specific module by using its name.
• The import statement creates a reference to that module in the current namespace.
After using import statement we can refer the things defined in that module.
• We can import the definitions inside a module to another module or the interactive
interpreter in Python. We use the import keyword to do this.
• Create second file. Let p2.py in same directory where p1.py is created. Write following
code in p2.py.
• Import the definitions inside a module:
import p1
print(p1.add(10,20))
print(p1.sub(20,10))
Output:
30
10
• Import the definitions using the interactive interpreter:
>>> import p1
>>> p1.add(10,20)
30
>>> p1.sub(20,10)
10
>>>
Importing Objects from Module:
• Import statement in python is similar to #include header_file in C/C++. Python modules
can get access to code from another module by importing the file/function using
import. Python provides three different ways to import modules.
1. From x import a:
• Imports the module x, and creates references in the current namespace to all public
objects defined by that module. If we run this statement, we can simply use a plain
name to refer to things defined in module x.
• We can access attribute/method directly without dot notation.
Example 1: Import inside a module (from x import a).
from p1 import add
print("Addition=" , add(10,20))
Output:
Addition= 30
4.5
Python Programming Modules, Working with Files and Exception Handling

Example 2: For import on interactive interpreter.


>>> from math import pi
>>> pi
3.141592653589793
>>> from math import sqrt
>>> sqrt(144)
12.0
2. From x import a, b, c:
• Imports the module x and creates references in the current namespace to the given
objects. Or we can use a, b and c function in our program.
Example 1: Import inside a module (from x import a, b, c).
from p1 import add, sub
print("Addition=" , add(10,20))
print("Subtraction=" ,sub(10,20))
Output:
Addition= 30
Subtraction= -10
Example 2: For import on interactive interpreter.
>>> from math import sqrt, ceil, floor
>>> sqrt(144)
12.0
>>> ceil(2.6)
3
>>> floor(2.6)
2
3. From x import *:
• We can use * (asterisk) operator to import everything from the module.
Example 1: Import inside a module (from x import *).
from p1 import *
print("Addition=" , add(10,20))
print("Subtraction=" ,sub(10,20))
print("Multiplication=" ,mul(10,20))
print("division=" ,div(10,20))
Output:
Addition= 30
Subtraction= -10
Multiplication= 200
division= 0.5
4.6
Python Programming Modules, Working with Files and Exception Handling

Example 2: For import on interactive interpreter.


>>> from math import *
>>> cos(60)
-0.9524129804151563
>>> sin(60)
-0.3048106211022167
>>> tan(60)
0.320040389379563

4.1.3 Aliasing Modules


• It is possible to modify the names of modules and their functions within Python by
using the ‘as’ keyword.
• We can make alias because we have already used the same name for something else in
the program or we may want to shorten a longer name.
Syntax: import module as another_name
Example: Create a module to define two functions. One to print Fibonacci series and
other for finding whether the given number is palindrome or not.
Step 1: Create a new file p1.py and write the following code in it and save it.
def add(a, b):
"This function adds two numbers and return the result"
result = a + b
return result

def sub(a, b):


"This function subtract two numbers and return the result"
result = a - b
return result

def mul(a, b):


"This function multiply two numbers and return the result"
result = a * b
return result

def div(a, b):


"This function divide two numbers and return the result"
result = a / b
return result
Step 2: Create new file p2.py to include the module. Add the following code and save
it.
import p1 as m
print("Addition=" , m.add(10,20))
print("Subtraction=" ,m.sub(10,20))
print("Multiplication=" ,m.mul(10,20))
print("division=" ,m.div(10,20))
4.7
Python Programming Modules, Working with Files and Exception Handling

Step 3: Execute p2.py file.


Addition= 30
Subtraction= -10
Multiplication= 200
division= 0.5

4.1.4 Predefined Modules


• A module is a collection of Python objects such as functions, classes, and so on. Python
interpreter is bundled with a standard library consisting of large number of built-in
modules,
• Built-in modules are generally written in C and bundled with Python interpreter in
precompiled form. A built-in module may be a Python script (with .py extension)
containing useful utilities.
• A module may contain one or more functions, classes, variables, constants, or any
other Python resources.
(I) Numeric and Mathematical Modules:
• This module provides numeric and math-related functions and data types. Following
are the modules which are classified as numeric and mathematical modules:
(i) numbers (Numeric abstract base classes).

(ii) math (Mathematical functions).


(iii) cmath (Mathematical functions for complex numbers).

(iv) decimal (Decimal fixed point and floating point arithmetic).

(v) fractions (Rational numbers).


(vi) random (Generate pseudo-random numbers).

(vii) statistics (Mathematical statistics functions).

• The numbers module defines an abstract hierarchy of numeric types.


The math and cmath modules contain various mathematical functions for floating-
point and complex numbers. The decimal module supports exact representations of
decimal numbers, using arbitrary precision arithmetic.
1. math and cmath Modules:

• Python provides two mathematical modules namely math and cmath.


• The math module gives us access to hyperbolic, trigonometric, and logarithmic
functions for real numbers and cmath module allows us to work with mathematical
functions for complex numbers.
4.8
Python Programming Modules, Working with Files and Exception Handling

Example 1: For math module.


>>> import math
>>> math.ceil(1.001)
2
>>> from math import *
>>> ceil(1.001)
2
>>> floor(1.001)
1
>>> factorial(5)
120
>>> trunc(1.115)
1
>>> sin(90)
0.8939966636005579
>>> cos(60)
-0.9524129804151563
>>> exp(5)
148.4131591025766
>>> log(16)
2.772588722239781
>>> log(16,2)
4.0
>>> log(16,10)
1.2041199826559246
>>> pow(144,0.5)
12.0
>>> sqrt(144)
12.0
>>>
• The mathematical functions for complex numbers.
Example 2: For cmath module.
>>> from cmath import *
>>> c=2+2j
>>> exp(c)
(-3.074932320639359+6.71884969742825j)
>>> log(c,2)
(1.5000000000000002+1.1330900354567985j)
>>> sqrt(c)
(1.5537739740300374+0.6435942529055826j)
2. Decimal Module:
• Decimal numbers are just the floating-point numbers with fixed decimal points. We
can create decimals from integers, strings, floats, or tuples.
• A Decimal instance can represent any number exactly, round up or down, and apply a
limit to the number of significant digits.
4.9
Python Programming Modules, Working with Files and Exception Handling

Example : For decimal module.


>>> from decimal import Decimal
>>> Decimal(121)
Decimal('121')
>>> Decimal(0.05)
Decimal('0.05000000000000000277555756156289135105907917022705078125')
>>> Decimal('0.15')
Decimal('0.15')
>>> Decimal('0.012')+Decimal('0.2')
Decimal('0.212')
>>> Decimal(72)/Decimal(7)
Decimal('10.28571428571428571428571429')
>>> Decimal(2).sqrt()
Decimal('1.414213562373095048801688724')
3. Fractions Module:
• A fraction is a number which represents a whole number being divided into multiple
parts.
• Python fractions module allows us to manage fractions in our Python programs.
• Example: For fractions module.
>>> import fractions
>>> for num, decimal in [(3, 2), (2, 5), (30, 4)]:
fract = fractions.Fraction(num, decimal)
print(fract)
3/2
2/5
15/2
• It is also possible to convert a decimal into a Fractional number. Let’s look at a code
snippet:
>>> import fractions
>>> for deci in ['0.6', '2.5', '2.3', '4e-1']:
fract = fractions.Fraction(deci)
print(fract)
Output:
3/5
5/2
23/10
2/5
>>>
4. Random Module:
• Sometimes, we want the computer to pick a random number in a given range, pick a
random element from a list etc.
• The random module provides functions to perform these types of operations. This
function is not accessible directly, so we need to import random module and then we
need to call this function using random static object.
4.10
Python Programming Modules, Working with Files and Exception Handling

• Example: For random module.


>>> import random
>>> print(random.random()) # It generate a random number in the range
(0.0, 1.0)
0.27958089234907935
>>> print(random.randint(10,20)) # It generate a random integer between x
and y inclusive
13
• Python uses library random to generate random numbers.
>>> import random
Methods:
(i) Randrange() Method: Generates integer between lower to upper argument. By
default lower bound is 0.
Example:
rno=random.randrange(50) #generates random numbers from 0 to 49
rno=random.randrange(10,20) #generates random numbers from 10 to 20
(ii) shuffle(list) Method: Used to shuffle the contents of the list.
Example:
import random
color = ['Cyan', 'Magenta', 'Yelow', 'Black']
random.shuffle(Color)
print "Reshuffled color :", Color
#which returns: Reshuffled color : ['Cyan', 'Magenta', 'Yelow', 'Black']
random.shuffle(Color)
print "Reshuffled color :", Color
#which returns: Reshuffled color : ['Yellow, 'Black', 'Magenta', 'Cyan']
(iii) uniform() Method: Returns a random floating number between the two
specified numbers (both included).
Syntax: random.uniform(x,y)
Where, x is lower limit and y is upper limit of random float and returns random
floating point number greater than or equal to x and less than y.
Example:
import random
print “uniform no(1,100):”, random.uniform(1,100)
# uniform no(1,100) 9.38500684602
(iv) random() Method: Tenerates the random numbers from 0 to 1.
Example:
>>> from random import random
>>> random() # 0.8407436368337916
>>> random() # 0.3069951291690556
>>> random() # 0.4887997856049814
>>> random() # 0.3534343245685684
Each time random generates the different number.
4.11
Python Programming Modules, Working with Files and Exception Handling

(v) randint() Method: Returns a random number selected element from the
specified range.
Example:
import random
print random.randint(0,5) #output either 1,2,3,4 or 5
(vi) choice() Method: Returns a random element from the given sequence.
Example:
import random
MyChoice = random.choice(['1-Swimming', '2-Badminton',
'3-Cricket', '4-Basketball', '5-Hockey'])
print 'My choice is:', MyChoice #returns: My choice is :
4-Basketball
5. Statistics Module:
• Statistics module provides access to different statistics functions. Example includes
mean (average value), median (middle value), mode (most often value), standard
deviation (spread of values).
Example: For statistics module.
>>> import statistics
>>> statistics.mean([2,5,6,9]) # average
5.5
>>> import statistics
>>> statistics.median([1,2,3,8,9]) # central value
3
>>> statistics.median([1,2,3,7,8,9])
5.0
>>> import statistics
>>> statistics.mode([2,5,3,2,8,3,9,4,2,5,6]) # repeated value
2
>>> import statistics
>>> statistics.stdev([1,1.5,2,2.5,3,3.5,4,4.5,5])
1.3693063937629153
(II) Functional Programming Modules:
• These modules provide functions and classes that support a functional programming
style and general operations on callable.
• Following are modules which comes under functional programming modules.
(i) itertools (functions creating iterators for efficient looping).
(ii) functools (higher-order functions and operations on callable objects).
(iii) operator (standard operators as functions).
1. itertools Module:
• Python programming itertools module provide us various ways to manipulate the
sequence while we are traversing it.
• Python itertools chain() function just accepts multiple iterable and return a single
sequence as if all items belongs to that sequence.

4.12
Python Programming Modules, Working with Files and Exception Handling

• Example: For itertools module with chain().


>>> from itertools import *
>>> for value in chain([1.3, 2.51, 3.3], ['C++', 'Python', 'Java']):
print(value)
Output:
1.3
2.51
3.3
C++
Python
Java
• Python itertools cycle() function iterate through a sequence upto infinite. This works
just like a circular Linked List.
• Example: For intools module with cycles().
>>> from itertools import *
>>> for item in cycle(['C++','Python','Java']):
index=index+1
if index==10:
break
print(item)
Output:
C++
Python
Java
C++
Python
Java
C++
Python
Java
>>>
2. functools Module:
• Python functools module provides us various tools which allows and encourages us to
write reusable code.
• Python functools partial() functions are used to replicate existing functions with some
arguments already passed in. It also creats new version of the function in a well-
documented manner.
• Suppose we have a function called multiplier which just multiplies two numbers. Its
definition looks like:
def multiplier(x, y):
return x * y
4.13
Python Programming Modules, Working with Files and Exception Handling

• Now, if we want to make some dedicated functions to double or triple a number then
we will have to define new functions as:
def multiplier(x, y):
return x * y
def doubleIt(x):
return multiplier(x, 2)
def tripleIt(x):
return multiplier(x, 3)
• But what happens when we need 1000 such functions? Here, we can use partial
functions:
from functools import partial
def multiplier(x, y):
return x * y
double = partial(multiplier, y=2)
triple = partial(multiplier, y=3)
print('Double of 2 is {}'.format(double(5)))
print('Triple of 5 is {}'.format(triple(5)))
Output:
Double of 5 is 10
Triple of 5 is 15
3. Operator Module:
• The operator module supplies functions that are equivalent to Python’s operators.
These functions are handy in cases where callables must be stored, passed as
arguments, or returned as function results.
• Functions supplied by the operator module are listed in following table:
Sr.
Function Signature/Syntax Behaves Like
No.
1. abs abs(a) abs(a)
2. add add(a,b) a+b
3. and_ and_(a,b) a&b
4. div div(a,b) a/b
5. eq eq(a,b) a==b
6. gt gt(a,b) a>b
7. invert, inv invert(a), inv(a) ~a
8. le le(a,b) a<=b
9. lshift lshift(a,b) a<<b
10. lt lt(a,b) a<b
11. mod mod(a,b) a%b
contd. …
4.14
Python Programming Modules, Working with Files and Exception Handling

12. mul mul(a,b) a*b


13. ne ne(a,b) a!=b
14. neg neg(a) -a
15. not_ not_(a) not a
16. or_ or_(a,b) a|b
17. pos pos(a) +a
18. repeat repeat(a,b) a*b
19. rshift rshift(a,b) a>>b
20. xor_ xor(a,b) a^b
4. Time Module:
• There is a popular time module available in Python which provides functions for
working with times and for converting between representations.
• Python has a module named time to handle time-related tasks. To use functions
defined in the module, we need to import the module first like import time.
• Python “time” module has following functions:
(i) time() : This function returns the current time instant, a floating-point number
of seconds since the epoch. Example. The function time() returns the current
system time in ticks since 00:00:00 hrs January 1, 1970 (epoch).
import time; # This is required to include time module.
ticks = time.time()
print "Number of ticks since 12:00am, January 1, 1970:", ticks
#Output Number of ticks since 12:00am, January 1, 1970: 7186862.73399
(ii) gmtime(sec) : This function returns a structure with 9 values each representing
a time attribute in sequence. It converts seconds into time attributes(days, years,
months etc.) till specified seconds from epoch. If no seconds are mentioned, time
is calculated till present.
(iii) asctime(“time”) : This function takes a time attributed string produced by
gmtime() and returns a 24 character string denoting time.
(iv) ctime(sec) : This function returns a 24 character time string but takes seconds as
argument and computes time till mentioned seconds. If no argument is passed,
time is calculated till present.
(v) sleep(sec) : This method is used to halt the program execution for the time
specified in the arguments.
(vi) tzset(): Resets the time conversion rules used by the library routines. The
environment variable TZ specifies how this is done.
(vii) clock(): Returns the current CPU time as a floating-point number of seconds. To
measure computational costs of different approaches, the value of time.clock is
more useful than that of time.time().
4.15
Python Programming Modules, Working with Files and Exception Handling

Datetime Module:
• A date in Python is not a data type of its own, but we can import a module
named datetime to work with dates as date objects.
Example: Import the datetime module and display the current date:
import datetime
x = datetime.datetime.now()
print(x)
Output:
2021-07-24 12:41:12.701464
• The date contains year, month, day, hour, minute, second, and microsecond.
The datetime module has many methods to return information about the date object.
Example: Return the year and name of weekday:
import datetime
x = datetime.datetime.now()
print(x.year)
print(x.strftime("%A"))
Output:
2021
Saturday
Creating Date Objects:
• To create a date, we can use the datetime() class (constructor) of the datetime module.
• The datetime() class requires three parameters to create a date: year, month, day.
Example: Create a date object:
import datetime
x = datetime.datetime(2020, 5, 17)
print(x)
Output:
2021-07-24 00:00:00

4.2 PACKAGES
• Suppose we have developed a very large application that includes many modules. As
the number of modules grows, it becomes difficult to keep track of them all as they
have similar names or functionality.
• It is necessary to group and organize them by some mean which can be achieved by
packages.
• A package is a hierarchical file directory structure that defines a single Python
application environment that consists of modules and subpackages and sub-
subpackages and so on.
• Packages allow for a hierarchical structuring of the module namespace using dot
notation. Packages are a way of structuring many packages and modules which help
in a well-organized hierarchy of data set, making the directories and modules easy to
access.
• A package is a collection of Python modules, i.e., a package is a directory of Python
modules containing an additional _ _init_ _.py file (For example: Phone/_ _init_ _.py).
4.16
Python Programming Modules, Working with Files and Exception Handling

4.2.1 Creating and Importing Packages


• Creating a package is quite easy, since it makes use of the operating system’s inherent
hierarchical file structure as shown in Fig. 4.5.
• Here, there is a directory named mypkg that contains two modules, p1.py and p2.py.
The contents of the modules are:
p1.py
def m1():
print("first module")
p2.py
def m2():
print("second module")
mypkg

p1.py

p2.py

Fig. 4.1
• Here, mypkg a folder /directory which consist of p1.py and p2.py. We can refer these
two modules with dot notation (mypkg.p1, mypkg.p2) and import them with the one of
the following syntaxes:
Syntax 1: import <module_name>[, <module_name> ...]
Example:
>>> import mypkg.p1,mypkg.p2
>>> mypkg.p1.m1()
first module
>>> p1.m1()
Syntax 2: from <module_name> import <name(s)>
Example:
>>> from mypkg.p1 import m1
>>> m1()
first module
>>>
Syntax 3: from <module_name> import <name> as <alt_name>
Example:
>>> from mypkg.p1 import m1 as function
>>> function()
first module
>>>
4.17
Python Programming Modules, Working with Files and Exception Handling

Syntax 4: from <package_name> import <modules_name>[, <module_name> ...]


Example:
>>> from mypkg import p1,p2
>>> p1.m1()
first module
>>> p2.m2()
second module
>>>

4.2.2 Standard Packages


• NumPy and SciPy are the standards packages used by Python programming.
• NumPy enriches the programming language Python with powerful data structures,
implementing multi-dimensional arrays and matrices.
• SciPy (Scientific Python) is often mentioned in the same breath with NumPy. SciPy
needs Numpy, as it is based on the data structures of Numpy and furthermore its basic
creation and manipulation functions.
• It extends the capabilities of NumPy with further useful functions for minimization,
regression, Fourier-transformation and many others.
• Both NumPy and SciPy are not part of a basic Python installation. They have to be
installed after the Python installation. NumPy has to be installed before installing
SciPy.
Math:
• Some of the most popular mathematical functions are defined in the math module.
These include trigonometric functions, representation functions, logarithmic functions
and angle conversion functions.
• Two mathematical constants are also defined in math module.
• The Pie (π
π) is a well-known mathematical constant, which is defined as the ratio of the
circumference to the diameter of a circle and its value is 3.141592653589793.
>>> import math
>>> math.pi
3.141592653589793
>>>
• Another well-known mathematical constant defined in the math module is e. It is
called Euler's number and it is a base of the natural logarithm. Its value is
2.718281828459045.
>>> import math
>>> math.e
2.718281828459045
>>>
4.18
Python Programming Modules, Working with Files and Exception Handling

NumPy:
• NumPy is the fundamental package for scientific computing with Python. NumPy
stands for "Numerical Python". It provides a high-performance multidimensional
array object, and tools for working with these arrays.
• An array is a table of elements (usually numbers), all of the same type, indexed by a
tuple of positive integers and represented by a single variable. NumPy's array class is
called ndarray. It is also known by the alias array.
• In NumPy arrays, the individual data items are called elements. All elements of an
array should be of the same type. Arrays can be made up of any number of
dimensions.
• In NumPy, dimensions are called axes. Each dimension of an array has a length which
is the total number of elements in that direction.
• The size of an array is the total number of elements contained in an array in all the
dimension. The size of NumPy arrays are fixed; once created it cannot be changed
again.
• Numpy arrays are great alternatives to Python Lists. Some of the key advantages of
Numpy arrays are that they are fast, easy to work with, and give users the opportunity
to perform calculations across entire arrays.
SciPy:
• SciPy is a library that uses NumPy for more mathematical functions. SciPy uses
NumPy arrays as the basic data structure, and comes with modules for various
commonly used tasks in scientific programming, including linear algebra, integration
(calculus), ordinary differential equation solving, and signal processing.
Matplotlib:
• The matplotlib.pyplot is a plotting library used for 2D graphics in python
programming language. It can be used in python scripts, shell, web application servers
and other graphical user interface toolkits.
• There are various plots which can be created using python matplotlib like bar graph,
histogram, scatter plot, area plot, pie plot.
Pandas:
• Pandas is an open-source Python Library providing high-performance data
manipulation and analysis tool using its powerful data structures.
• It is built on the Numpy package and its key data structure is called the DataFrame.
DataFrames allow you to store and manipulate tabular data in rows of observations
and columns of variables.

4.2.3 User Defined Packages


• We organize a large number of files in different folders and subfolders based on some
criteria, so that we can find and manage them easily.
• In the same way, a package in Python takes the concept of the modular approach to
next logical level.
4.19
Python Programming Modules, Working with Files and Exception Handling

• As we know, a module can contain multiple objects, such as classes, functions, etc.
A package can contain one or more relevant modules.
• Physically, a package is actually a folder containing one or more module files. Let's
create a package named MyPkg, using the following steps:
Step 1: Create a folder MyPkg on,
C:\Users\Meenakshi\AppData\Local\Programs\Python\Python37\”.
Create modules Message.py and Mathematics.py with following code:
Message.py
def SayHello(name):
print("Hello " + name)
return
Mathematics.py
def sum(x,y):
return x+y
def average(x,y):
return (x+y)/2
def power(x,y):
return x**y
Step 2: Create an empty _ _init_ _.py file in the MyPkg folder. The package folder
contains a special file called _ _init_ _.py, which stores the package's content. It serves
two purposes:
(i) The Python interpreter recognizes a folder as the package if it contains
_ _init_ _.py file.
(ii) _ _init_ _.py exposes specified resources from its modules to be imported.
An empty _ _init_ _.py file makes all functions from above modules available when this
package is imported. Note that _ _init_ _.py is essential for the folder to be recognized
by Python as a package. We can optionally define functions from individual modules
to be made available.
Step 3: Create P1.py file in MyPkg folder and write following code:
from MyPkg import Mathematics
from MyPkg import Message
greet.SayHello("Meenakshi")
x=functions.power(3,2)
print("power(3,2) : ", x)
Output:
Hello Meenakshi
power(3,2) : 9
4.20
Python Programming Modules, Working with Files and Exception Handling

Using _ _init_ _.py File:


• The _ _init_ _.py file is normally kept empty. However, it can also be used to choose
specific functions from modules in the package folder and make them available for
import. Modify _ _init_ _.py as below:
_ _init_ _.py
from .Mathematics import average, power
from .Message import SayHello
• The specified functions can now be imported in the interpreter session or another
executable script.
• Create test.py in the MyPkg folder and write following code:
test.py from MyPkg import power, average, SayHello
SayHello()
x=power(3,2)
print("power(3,2) : ", x)
• Note that functions power() and SayHello() are imported from the package and not
from their respective modules, as done earlier. The output of above script is:
Hello world
power(3,2) : 9
4.2.4 Examples
• We have included a __init__.py, file inside a directory to tell Python that the current
directory is a package.
• Whenever we want to create a package, then we have to include __init__.py file in the
directory. we can write code inside it.
• Let's create a simple package that has the following structure:
Package (university)
o __init__.py
o student.py
o faculty.py
# student.py
class Student:
def __init__(self, student):
self.name = student['name']
self.gender = student['gender']
self.year = student['year']
def get_student_details(self):
return f"Name: {self.name}\nGender: {self.gender}\nYear: {self.year}"
4.21
Python Programming Modules, Working with Files and Exception Handling

# faculty.py
class Faculty:
def __init__(self, faculty):
self.name = faculty['name']
self.subject = faculty['subject']
def get_faculty_details(self):
return f"Name: {self.name}\nSubject: {self.subject}"

• We have the above in the student.py and faculty.py files. Let's create another file to
access those classed inside it. Now, inside the package directory create a file
named testing.py and include the following code:
# testing.py
# importing the Student and Faculty classes from respective files
from student import Student
from faculty import Faculty
# creating dicts for student and faculty
student_dict = {'name' : 'John', 'gender': 'Male', 'year': '3'}
faculty_dict = {'name': 'Emma', 'subject': 'Programming'}
# creating instances of the Student and Faculty classes
student = Student(student_dict)
faculty = Faculty(faculty_dict)
# getting and printing the student and faculty details
print(student.get_student_details())
print()
print(faculty.get_faculty_details())
Output:
Name: John
Gender: Male
Year: 3
Name: Emma
Subject: Programming
• We have seen how to create and to access a package in Python. And this is a simple
package. There might be plenty of sub-packages and files inside a package. Let's see
how to access subpackage modules.
4.22
Python Programming Modules, Working with Files and Exception Handling

• Create a directory with the following structure:


package (university)

o __init__.py

o Subpackage (student)

__init__.py

main.py

...

o testing.py
• Copy the above student code and place it here. Now, let's see how to access it in
the testing.py file. Add the following in the testing.py file.
Example:
# testing.py
from student.main import Student
# creating dicts for student
student_dict = {'name' : 'John', 'gender': 'Male', 'year': '3'}
# creating instances of the Student class
student = Student(student_dict)
# getting and printing the student details
print(student.get_student_details())
Output: After running testing.py.
Name: John
Gender: Male
Year: 3
Example: For NumPy with array object.
>>> import numpy as np
>>> a=np.array([1,2,3]) # one dimensional array
>>> print(a)
[1 2 3]
>>> arr=np.array([[1,2,3],[4,5,6]]) # two dimensional array
>>> print(arr)
[[1 2 3]
[4 5 6]]
>>> type(arr)
<class 'numpy.ndarray'>
>>> print("No. of dimension: ", arr.ndim)
4.23
Python Programming Modules, Working with Files and Exception Handling

No. of dimension: 2
>>> print("Shape of array: ", arr.shape)
Shape of array: (2, 3)
>> >print("size of array: ", arr.size)
size of array: 6
>>> print("Type of elements in array: ", arr.dtype)
Type of elements in array: int32
>>> print("No of bytes:", arr.nbytes)
No of bytes: 24
Example 1: Using linalg sub package of SciPy.
>>> import numpy as np
>>> from scipy import linalg
>>> a = np.array([[1., 2.], [3., 4.]])
>>> linalg.inv(a) # find inverse of array
array([[-2. , 1. ],
[ 1.5, -0.5]])
>>>
Example 2: Using linalg sub package of SciPy.
>>> import numpy as np
>>> from scipy import linalg
>>> a = np.array([[1,2,3], [4,5,6], [7,8,9]])
>>> linalg.det(a) # find determinant of array
0.0
>>>
Example 3: For line plot.
>>> from matplotlib import pyplot as plt
>>> x=[2,4,8,10]
>>> y=[2,8,8,2]
>>> plt.plot(x,y)
[<matplotlib.lines.Line2D object at 0x02E69B70>]
>>> plt.show()
Output:

4.24
Python Programming Modules, Working with Files and Exception Handling

Example 4: For bar graph.


>>> from matplotlib import pyplot as plt
>>> x=[2,4,8,10]
>>> y=[2,8,8,2]
>>> plt.xlabel('X-Axis')
Text(0.5, 0, 'X-Axis')
>>> plt.ylabel('Y-Axis')
Text(0, 0.5, 'Y-Axis')
>>> plt.title('Graph')
Text(0.5, 1.0, 'Graph')
>>> plt.bar(x,y,label="Graph",color='r',width=.5)
<BarContainer object of 4 artists>
>>> plt.show()
Output:

Example 5: Using series data structure of Panda.


>>> import pandas as pd
>>> import numpy as np
>>> numpy_arr = array([2, 4, 6, 8, 10, 20])
>>> si = pd.Series(arr)
>>> print(si)
0 2
1 4
2 6
3 8
4 10
5 20
dtype: int32
Example 6: Using series data structure of Panda.
>>> import pandas as pd
>>> data=[10,20,30,40,50]
4.25
Python Programming Modules, Working with Files and Exception Handling

>>> index=['a','b','c','d','e']
>>> si=pd.Series(data,index)
>>> si
a 10
b 20
c 30
d 40
e 50
dtype: int64
>>>

4.3 WORKING WITH FILES


• File is a named location on disk to store related information. It is used to permanently
store data in a non-volatile memory (e.g. hard disk).
• Since, Random Access Memory (RAM) is volatile which loses its data when computer is
turned off. We use files for future use of the data permanently.
• To read from or write to a file, we must first open it when we are done with it, we
should close it to free up the resources it holds.
• Python has several functions for file handling to perform operations like creating,
reading, updating, and deleting files.
• Python has extensive support for manipulating files and directories. These
manupulations can be carried out by operating system commands using the built-in
Python functions for file and directory.
• Files are divided into following two categories:
1. Text Files: Text files are simple texts in human readable format. A text file is
structured as sequence of lines of text.
2. BinaryFiles: Binary files have binary data (0s and 1s) which is understood by the
computer.
• When we want to read from or write to a file we need to open it first. When we are
done, it needs to be closed, so that resources that are tied with the file are freed.
• Hence, in Python a file operation takes place in the following order:
o Open a file.
o Read or write (perform operation).
o Close the file.

4.3.1 Creating Files


• Python provides basic functions and methods necessary to manipulate files. The file
manipulation is performed using a file object.
4.26
Python Programming Modules, Working with Files and Exception Handling

• To create a new file in Python, use the open() method, with one of the following
parameters.
"x" - create - will create a file, returns an error if the file exist.
"a" - append - will create a file if the specified file does not exist.
"w" - write - will create a file if the specified file does not exist.
Example: Create a file called "xyz.txt":
f = open("xyz.txt", "x") # Here a new empty file is created
Example: Create a new file if it does not exist:
f = open("xyz.txt", "w")
Write to an Existing File
• To write to an existing file, you must add a parameter to the open() function
"a" - Append - will append to the end of the file.
"w" - Write - will overwrite any existing content.
Example: Open the file "demofile.txt" and append content to the file:
f = open("abc.txt", "a")
f.write("Now you can append line to a file")
Example:
file = open("testfile.txt","w")
file.write("Hello World")
file.write("This is our new text file")
file.write("and this is another line.")
file.write("Why? Because we can.")
• There are two types of files that can be handled in python, normal text files and binary
files.
o Text files: In this type of file, Each line of text is terminated with a special
character called EOL (End of Line), which is the new line character (‘\n’) in Python
by default.
o Binary files: In this type of file, there is no terminator for a line and the data is
stored after converting it into machine understandable binary language.
Accessing File Contents using Standard Library Functions:
• Once, a file is opened and we will have one file object, we can get various information
related to that file. Here, is a list of all attributes related to file object:
Sr.
Attribute Description Example
No.
1. file.closed Returns true if file is closed, >>> f=open("abc.txt")
false otherwise. >>> f.closed
False
contd. …
4.27
Python Programming Modules, Working with Files and Exception Handling

2. file.mode Returns access mode with >>> f=open("abc.txt","w")


which file was opened. >>> f.mode
'w'
3. file.name Returns name of the file. >>> f=open("abc.txt","w")
>>>f.name
'abc.txt'
4. file.encoding The encoding of the file. >>> f=open("abc.txt","w")
>>>f.encoding
cp1252
Example: For file object attribute.
f=open("sample.txt","r")
print(f.name)
print(f.mode)
print(f.encoding)
f.close()
print(f.closed)
Output:
sample.txt
r
cp1252
True
4.3.2 Operations on Files
• Python too supports file handling and allows users to handle files i.e., to read and
write files, along with many other file handling options, to operate on files.
• This section describes the file operations of all types of files include opening a file,
reading from a file, writing to a file, and closing a file.
1. open() Function:
• We have to open the file before you can read or write a file, using Python's built-in
open() function.
• The open() function creates a file object which is called "handle". This would be
utilized to call other support methods associated with it.
Syntax: file_object = open(filename [, access_mode][, buffering])
where,
o filename: The file_name argument is a string value that contains the name of the
file that you want to access.
4.28
Python Programming Modules, Working with Files and Exception Handling

o access_mode: The access_mode determines the mode in which the file has to be
opened, i.e., read, write, append, etc. A complete list of possible values is given
below in the table. This is an optional parameter and the default file access mode
is read (r).
o buffering: If the buffering value is set to 0, no buffering takes place. If the
buffering value is 1, line buffering is performed while accessing a file. If you
specify the buffering value as an integer greater than 1, then buffering action is
performed with the indicated buffer size. If negative, the buffer size is the system
default(default behavior).
• The file can be open in Text mode or Binary mode.
(i) The text mode returns strings while reading from the file. The default is reading
in text mode.
(ii) The binary mode returns bytes and this is the mode to be used when dealing with
non-text files like image or executable files.
• The text and binary modes are used in conjunction with the r, w, and a modes. The list
of all the modes used in Python are given in following table:
Sr.
Mode Description
No.
1. r Opens a file for reading only. The file pointer is placed at the
beginning of the file. This is the default mode.
2. 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.
3. r+ Opens a file for both reading and writing. The file pointer placed at
the beginning of the file.
4. rb+ Opens a file for both reading and writing in binary format. The file
pointer placed at the beginning of the file.
5. 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.
6. 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.
7. 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.
8. 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.
contd. …
4.29
Python Programming Modules, Working with Files and Exception Handling

9. 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.
10. 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.
11. 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.
12. ab+ Opens a file for both appending and reading in binary format. The file
pointer is at the end of the file if the file exists. The file opens in the
append mode. If the file does not exist, it creates a new file for reading
and writing.
13. t Opens in text mode (default).
14. b Opens in binary mode.
15. + Opens a file for updating (reading and writing).
Example: For different modes of opening a file.
f = open("test.txt") # opening in r mode (reading only)
f = open("test.txt",'w') # opens in w mode (writing mode)
f = open("img.bmp",'rb+') # read and write in binary mode
2. close() Function:
• When the all operations related to the file are complete, then we need to close the file.
The close() function of a file object flushes any unwritten information and closes the
file object, after which no more writing can be done.
• Python automatically closes a file when the reference object of a file is reassigned to
another file. It is a good practice to use the close() function to close a file.
Syntax: fileObject.close()
Example:
# Open a file
f = open("test.txt", "w")
print ("Name of the file: ", f.name)
# Close opened file
f.close()
Output:
Name of the file: test.txt
3. Reading and Writing Files:
• The methods read() and write() are used to read and write files.
4.30
Python Programming Modules, Working with Files and Exception Handling

Writing Data to File:


• In order to write into a file in Python, we need to open it in write 'w', append 'a' or
exclusive creation 'x' mode.
• We need to be careful with the 'w' mode as it will overwrite into the file if it already
exists. All previous data are erased. There are following two ways to write in a fil:.
Using write() Function:
• Writing a string or sequence of bytes (for binary files) is done using write()method.
This method returns the number of characters written to the file.
Example: Inserts the string str1 in a single line in the text file. The write() method
does not add a newline character ('\n') to the end of the string.
File_object.write(str1)
Example: For write(string) method.
f=open("sample.txt")
print("**content of file1**")
print(f.read())
f=open("sample.txt","w")
f.write("first line\n")
f.write("second line\n")
f.write("third line\n")
f.close()
f=open("sample.txt","r")
print("**content of file1**")
print(f.read())
Output:
**content of file1**
Hello,I am There
**content of file1**
first line
second line
third line
Using writelines() Function:
• It writes a sequence of strings to the file. The sequence can be any iterable object
producing strings, typically a list of strings. There is no return value.
4.31
Python Programming Modules, Working with Files and Exception Handling

• For a list of string elements, each string is inserted in the text file. Used to insert
multiple strings at a single time.
File_object.writelines(L) for L = [str1, str2, str3]
Example: For writelines() method.
fruits=["Orange\n","Banana\n","Apple\n"]
f=open("sample.txt",mode="w+",encoding="utf-8")
f.writelines(fruits)
f.close()
f=open("sample.txt","r")
print(f.read())
Output:
Orange
Banana
Apple
Reading Data From File:
• To read a file in python, we must open the file in reading mode (r or r+).
• There are following three ways to read data from a text file:
1. read(): Returns the read bytes in form of a string. Reads n bytes, if no n specified,
reads the entire file.
Syntax: File_object.read([n])
Example: for read() method.
f=open("sample.txt","r")
print(f.read(5)) # read first 5 data
print(f.read(5)) # read next five data
print(f.read()) # read rest of the file
print(f.read())
Output:
first
line
second line
third line
2. readline(): Reads a line of the file and returns in form of a string. For specified n,
reads at most n bytes. However, does not reads more than one line, even if n
exceeds the length of the line.
Syntax: File_object.readline([n])
4.32
Python Programming Modules, Working with Files and Exception Handling

Example: For readline() method.


f=open("sample.txt","r")
print(f.readline()) # read first line followed by\n
print(f.readline(3))
print(f.readline(5))
print(f.readline())
print(f.readline())
Output:
first line
sec
ond l
ine
third line
3. readlines(): Reads all the lines and return them as each line a string element in a
list.
Syntax: file_object.readlines()
Example: For roadlines() method.
f=open("sample.txt","r")
print(f.readlines())
Output:
['first line\n', 'second line\n', 'third line\n']
File Related Standard Functions:
Sr.
Function Description Example
No.
1. file.close() Close the file. We need to f.close()
reopen it for further access.
2. file.flush() Flush the internal buffer. f.flush()
print(f.read())
f.close()
3. file.fileno() Returns an integer file print(f.fileno())
descriptor. f.close()
4. file.isatty() It returns true if file has a print(f.isatty())
<tty> attached to it. f.close()
5. file.next() Returns the next line from try:
the last offset. whilef.next():
print(f.next())
except:
f.close()
contd. …
4.33
Python Programming Modules, Working with Files and Exception Handling

6. file.read() This function reads the lines =f.read()


entire file and returns a f.write(lines)
string. f.close()
7. file.read(size) Reads the given number of text =f.read(10)
bytes. It may read less if print(text)
EOF is hit. f.close()
8. file.readline() Reads a single line and text =f.readline()
returns it as a string. print(text)
f.close()
9. file.readline(size) It will read an entire line text =f.readline(20)
(trailing with a new line print(text)
char) from the file. f.close()
10. file.readlines() Reads the content of a file lines =f.readlines()
line by line and returns f.writelines(lines)
them as a list of strings. f.close()
11. file.readlines It calls the readline() to read text =f.readlines(25)
(size_hint) until EOF. It returns a list of print(text)
lines read from the file. If f.close()
you pass <size_hint>, then it
reads lines equalling the
<size_hint> bytes.
12. file.seek(offset Sets the file’s current position =f.seek(0,0);
[, from]) position. print(position)
f.close()
13. file.tell() Returns the file’s current lines =f.read(10)
position. #tell()
print(f.tell())
f.close()
14. file.truncate(size) Truncates the file’s size. If f.truncate(10)
the optional size argument f.close()
is present, the file is
truncated to (at most) that
size.
15. file.write(string) It writes a string to the file. line ='Welcome
And it doesn’t return any Geeks\n'
value. f.write(line)
f.close()
16. file.writelines Writes a sequence of strings lines =f.readlines()
(sequence) to the file. The sequence is #writelines()
possibly an iterable object f.writelines(lines)
producing strings, typically f.close()
a list of strings.
4.34
Python Programming Modules, Working with Files and Exception Handling

Example: For file object methods.


f = open("sample.txt","w+")
f.write("Line one\nLine two\nLine three")
f.seek(0)
print(f.read())
print("Is readable:",f.readable())
print("Is writeable:",f.writable())
print("File no:",f.fileno())
print("Is connected to tty-like device:",f.isatty())
f.truncate(5)
f.flush()
f.close()
Output:
Line one
Line two
Line three
Is readable: True
Is writeable: True
File no: 3
Is connected to tty-like device: False
Handling Files through OS Module:
• The OS module of Python allows us to perform Operating System (OS) dependent
operations such as making a folder, listing contents of a folder, know about a process,
end a process etc.
• It has methods to view environment variables of the operating system on which
Python is working on and many more.
Directory related Standard Functions:
Sr.
Function Description Example
No.
1. os.getcwd() Show current import os
working os.getcwd()
directory.
2. os.path.getsize() Show file size in size
bytes of file =os.path.getsize(“sample.txt”)
passed in
parameter.
3. os.path.isfile() Is passed print(os.path.isfile
parameter a file. ("sample.txt"))
contd. …
4.35
Python Programming Modules, Working with Files and Exception Handling

4. os.path.isdir() Is passed print(os.path.isdir("sample.txt"))


parameter a
folder.
5. os.listdir() Returns a list of print("***Contents of Present
all files and working directory
folders of present ***\n ",os.listdir())
working.
directory.
6. os.listdir(path) Return a list print("***Contents of given
containing the directory***\n",
names of the os.listdir(“testdir”))
entries in the
directory given
by path.

7. os.rename(current,new) os.rename("sample.txt",
Rename a file.
"sample1.txt")
8. os.remove(file_name) Delete a file. os.remove("sample.txt")
9. os.mkdir() Creates a single os.mkdir("testdir")
subdirectory.
10. os.chdir(path) Change the os.chdir("d:\IT")
current working
directory to
path.
Example: For handling files through OS module.
import os
os.getcwd()
print("***Contents of Present working directory***\n ", os.listdir())
print(os.path.isfile("sample.txt"))
print(os.path.isdir("sample.txt"))
***Contents of Present working directory***
['DLLs', 'Doc', 'etc', 'file1.txt', 'include', 'Lib', 'libs',
'LICENSE.txt', 'mypkg', 'NEWS.txt', 'p1.py', 'p2.py', 'python.exe',
'python3.dll', 'python37.dll', 'pythonw.exe', 'sample.txt', 'Scripts',
'share', 'tcl', 'test.py', 'Tools', 'vcruntime140.dll', '_ _pycache_ _']
True
False
4.36
Python Programming Modules, Working with Files and Exception Handling

4.3.3 File Object Attributes


• A file object allows us to use, access and manipulate all the user accessible files.
• One can read and write any such files. Once a file is opened and he/she have one file
object, he/she can get various information related to that file.
• Following is a list of all the attributes related to a file object:
Table 4.1: List of Attributes of file object
Sr. No. Attribute and Description
1. file.closed: Returns true if file is closed, false otherwise.
2. file.mode: Returns access mode with which file was opened.
3. file.name: Returns name of the file.
4. file.softspace: Returns false if space explicitly required with print, true
otherwise.
Example:
# Open a file
f = open("test.txt", "wb")
print ("Name of the file: ", f.name)
print ("Closed or not: ", f.closed)
print ("Opening mode: ", f.mode)
print("Soft space flag: ", f.softspace)
f.close()
Output:
Name of the file: test.txt
Closed or not: False
Opening mode: wb
Softspace flag: 0

4.3.4 File Positions


• To move directly to the position we use two built-in methods tell() and seek(). The tell()
method tells us the current position within the file. The seek() method changes the
current file position.
• We can change the current file cursor (position) using the seek() method. Similarly,
the tell() method returns the current position (in number of bytes) of file
cursor/pointer.
• To change the file object’s position use f.seek(offset, reference_point). The position is
computed from adding offset to a reference point.
• The reference_point can be omitted and defaults to 0, using the beginning of the file as
the reference point.
4.37
Python Programming Modules, Working with Files and Exception Handling

• The reference points are 0 (the beginning of the file and is default), 1 (the current
position of file) and 2 (the end of the file).
• The f.tell() returns an integer giving the file object’s current position in the file
represented as number of bytes from the beginning of the file when in binary
mode and an opaque number when in text mode.
• In other words, the tell() is used to find the current position of the file pointer in the
file while the seek() used to move the file pointer to the particular position.
Example 1: For file position.
f=open("sample.txt","r")
print(f.tell())
print(f.read())
print(f.tell())
print(f.read()) # print blank line
print(f.seek(0))
print(f.read())
Output:
0
first line
second line
third line

37

0
first line
second line
third line
>>>
Example 2:
fruits=["Orange\n","Banana\n","Apple\n"]
f=open("sample.txt",mode="w+",encoding="utf-8")
for fru in fruits:
f.write(fru)
print("Tell the byte at which the file cursor is:",f.tell())
f.seek(0)
for line in f:
print(line)
Output:
Tell the byte at which the file cursor is: 23
Orange
Banana
Apple
4.38
Python Programming Modules, Working with Files and Exception Handling

Example 3:
# Open a file
f = open("test.txt", "r+")
str = f.read(18)
print ("Read String is: ", str)
# Check current position
position = f.tell()
print ("Current file position: ", position)
# Reposition pointer at the beginning once again
position = f.seek(0, 0)
str = f.read(18)
print ("Again read String is: ", str)
# Close opened file
f.close()
Output:
Read String is: Python programming
Current file position: 18
Again read String is: Python programming

4.3.5 Listing Files in a Directory


• Python os module provides methods that help you perform file-processing operations,
such as renaming and deleting files.
• To use this module, we need to import it first and then we can call any related
functions.
• The shutil (or shell utilities) module has functions to let you copy, move, rename, and
delete files in your Python programs. To use the shutil functions, we will first need to
use import shutil.
• The os.listdir() is used to get a list of entries in a directory. Pass in the directory for
which you need the entries; use a "." for the current directory of the process.
Syntax: os.listdir(path)
where, path is the directory, which needs to be explored.
Return Value: This method returns a list containing the names of the files, directories
in the directory given by path. Path may be either of type str or type bytes.
Example:
import os, sys
#Open a file
path = "d:\\tybsc\\"

4.39
Python Programming Modules, Working with Files and Exception Handling

dirs = os.listdir( path )


# This would print all the files and directories
for file in dirs:
print (file)
Output: We will get list of all files in tybsc directory.

4.3.6 Testing File Types


• The os.path module includes functions that are useful to perform path-related oper-
ations.
• We can access the as os.path module from the os.module. The methods available in
this module are useful for operations such as file path inquiries and retrieving
information about files and directories.
• The functions isfile(), isdir(), and islink() in the os.path module are used to test if a
string reflects the name of a regular file, a directory, or a link.
1. os.path.isfile(path): Return True if path is an existing regular file.
2. os.path.isdir(path): Returns True if path is an existing directory.
3. os.path.islink(path): Returns True if path refers to a directory entry that is a
symbolic link.
Example:
import os.path
FILENAMES = [
file 1
os.path.dirname( file ), 1/1,
'./broken link',
]
for file in FILENAMES:
print('File : (!r}'.format(file))
print('Absolute : ', os.path.isabs(file))
print('Is File? : ', os.path.isfile(file))
print('Is Dir? : ', os.path.isdir(file))
print('Is Link? : ', os.path.islink(file))
print ('Mountpoint? : ', os.path.ismount(file))
print('Exists? : ', os.path.exists(file))
print('Link Exists? : ', os.path.lexists(file))
print()
4.40
Python Programming Modules, Working with Files and Exception Handling

Output:
File : '/'
Absolute : True
Is File? : False
Is Dir? : True
Is Link? : False
Mountpoint? : True
Exists? : True
Link Exists? : True
File : './broken_link'
Absolute : False
Is File? : False
Is Dir? : False
Is Link? : True
Mountpoint? : False
Exists? : False
Link Exists? : True

4.3.7 Removing Files and Directories


• We can use the remove() method to delete files by supplying the name of the file to be
deleted as the argument.
Syntax: os.remove(file_name)
Example:
import os
# Delete file test1.txt
os.remove("text1.txt")
Example:
import os
if os.path.exists("demofile.txt"):
os.remove("demofile.txt")
else:
print("The file does not exist")
rmdir() Method:
• The rmdir() method deletes or removes the directory, which is passed as an argument
in the method.
4.41
Python Programming Modules, Working with Files and Exception Handling

• Before removing a directory, all the contents in it should be removed.


Syntax: os.rmdir('dirname')
Example:
import os
# This would remove "/tmp/test" directory.
os.rmdir( "/tmp/test" )
Delete directory and contents recursively:
• Use shutil module rmtree() function for deleting contents and directory recursively.
Example: import shutil
shutil.rmtree("/home/ismail/cache")
4.3.8 Copying and Renaming Files
• Python provides in-built functions for easily copying files using the Operating System
Shell utilities. The shutil module offers a number of high-level operations on files and
collections of files.
• Python rename() file is a method used to rename a file or a directory in Python
programming.
Copying Files and Folders:
• Following are the ways through which Python copy a file.
1. Using Python OS Module:
• There are two ways to copy a file in Python- the popen() method and the system()
method.
(i) popen() Method:
• This method creates a pipe to or from the command. It returns an open file object
which connects to a pipe. We can use it for reading or writing according to the file
open mode i.e. ‘r’ (default) or ‘w’.
os.popen(command[, mode[, bufsize]])
where, mode: It can be ‘r’ (default) or ‘w’.
bufsize: If buffering value is 0, then no buffering will occur. If it is set to 1, then line
buffering will take place while accessing the file. If you provide a value greater than 1,
then buffering will occur with the specified buffer size. However, for a negative value,
the system will assume the default buffer size. This method returns an open file object
connected to the pipe.
For Windows OS:
import os
os.popen('copy 1.txt.py 2.txt.py')
4.42
Python Programming Modules, Working with Files and Exception Handling

For Linux OS:


import os
os.popen('cp 1.txt.py 2.txt.py')
(ii) system() Method:
• It is the most common way of running any system command. With the system()
method, you can call any command in a subshell. Internally, this method will call the
standard C library function.
• The system() method returns the exit status of the command.
For Windows OS.
import os
os.system('copy 1.txt.py 2.txt.py')
For Linux OS.
import os
os.system('cp 1.txt.py 2.txt.py')
2. Using Python Threading Library:
• We can borrow the Thread module from the threading library to Python copy a file.
This does the copying in an async manner.
• While using this method, please make sure to employ locking to avoid deadlocks. We
may face it if the application is using multiple threads reading/writing a file.
import shutil
from threading import Thread
src="1.txt.py"
dst="3.txt.py"
Thread(target=shutil.copy, args=[src, dst]).start()
3. Using Python Subprocess Module:
• The subprocess module lets us work with child processes- launch them, attach to their
pipes for input, output, and error, and retrieve return values.
• From this module, we can use the methods call() and check_output(), for Python copy a
file.
Subprocess’s call() Method to Copy a File in Python:
• The subprocess module gives a simple interface to work with child processes. It
enables us to launch subprocesses, attach to their input/output/error pipes, and
retrieve the return values.
• The subprocess module aims to replace the legacy modules and functions like –
os.system, os.spawn*, os.popen*, popen2.*.
4.43
Python Programming Modules, Working with Files and Exception Handling

• It exposes a call() method to invoke system commands to execute user tasks.


import subprocess
src="1.txt.py"
dst="2.txt.py"
cmd='copy "%s" "%s"' % (src, dst)
status = subprocess.call(cmd, shell=True)
if status != 0:
if status < 0:
print("Killed by signal", status)
else:
print("Command failed with return code - ", status)
else:
print('Execution of %s passed!\n' % cmd)
Subprocess’s check_output() Method to Copy a File in Python:
• With subprocess’s check_output() method, you can run an external command or a
program and capture its output. It also supports pipes.
import os, subprocess
src=os.path.realpath(os.getcwd()+"https://cdn.techbeamers.com/1.txt.py")
dst=os.path.realpath(os.getcwd()+"https://cdn.techbeamers.com/2.txt.py")
cmd='copy "%s" "%s"' % (src, dst)
status = subprocess.check_output(['copy', src, dst], shell=True)
print("status: ", status.decode('utf-8'))
4. Using Python Shutil Module:
• The shutil module provides functions for copying files, as well as entire folders.
• Calling shutil.copy(source, destination) will copy the file at the path source to the
folder at the path destination (both source and destination are strings).
• If destination is a filename, it will be used as the new name of the copied file. This
function returns a string of the path of the copied file.
• There are four methods we will consider in this module to help Python copy a file-
copyfile(), copy(), copy2(), and copyfileobj().
• This method copies the content of the source to the destination only if the target is
writable. If we don’t have the right permissions, then it will raise an IOError.
(i) shutil copyfile() Method:
• This method copies the content of the source to the destination only if the target is
writable. If we don’t have the right permissions, then it will raise an IOError.
• It works by opening the input file for reading while ignoring its file type. Next, it
doesn’t treat special files any differently and won’t copy them as new special files.
4.44
Python Programming Modules, Working with Files and Exception Handling

• The copyfile() method makes use of lower-level function copyfileobj() below. It takes
file names as arguments, opens them and passes file handles to copyfileobj().
• In this method, one optional third argument can use to specify the buffer length. It’ll
then open the file for reading in chunks of the specified buffer size. However, the
default behavior is to read the entire file in one go.
Syntax: copyfile(source_file, destination_file)
Example: Sample code for copy file.
from shutil import copyfile
from sys import exit
source = input("Enter source file with full path: ")
target = input("Enter target file with full path: ")
# adding exception handling
try:
copyfile(source, target)
except IOError as e:
print("Unable to copy file. %s" % e)
exit(1)
except:
print("Unexpected error:", sys.exc_info())
exit(1)
print("\nFile copy done!\n")
while True:
print("Do you like to print the file ? (y/n): ")
check = input()
if check == 'n':
break
elif check == 'y':
file = open(target, "r")
print("\nHere follows the file content:\n")
print(file.read())
file.close()
print()
break
else:
continue
4.45
Python Programming Modules, Working with Files and Exception Handling

(ii) copy() Method:


• The copy() method is like the "cp" command in Unix. It means if the target is a folder,
then it’ll create a new file inside it with the same name (basename) as the source file.
Syntax: shutil.copy(source, destination)
• Also, this method will sync the permissions of the target file with the source after
copying its content. It too throws the SameFileError if you are copying the same file.
import os
import shutil
#store the file path in the variable 'source' and 'target'
source = 'current/test/test.py'
target = '/prod/new'
assert not os.path.isabs(source)
target = os.path.join(target, os.path.dirname(source))
# create the folders if not already exists
os.makedirs(target)
# adding exception handling
try:
shutil.copy(source, target)
except IOError as e:
print("Unable to copy file. %s" % e)
except:
print("Unexpected error:", sys.exc_info())
(iii) Copyfileobj() Method:
• This method copies the file to a target path or file object. If the target is a file object,
then you need to close it explicitly after the calling the Copyfileobj().
• It assumes an optional argument (the buffer size) which we can use to supply the
buffer length. It is the number of bytes kept in memory during the copy process. The
default size that system uses is 16KB.
Example:
from shutil import copyfileobj
status = False
if isinstance(target, string_types):
target = open(target, 'wb')
status = True
try:
copyfileobj(self.stream, target, buffer_size)
finally:
if status:
target.close()
4.46
Python Programming Modules, Working with Files and Exception Handling

(iv) Shutil copy2() Method:


• However, the copy2() method functions like the copy(). But it also gets the access and
modification times added in the meta-data while copying the data.
• Copying the same file would result in SameFileError.
from shutil import *
import os
import time
from os.path import basename
def displayFileStats(filename):
file_stats = os.stat(basename(filename))
print('\tMode:', file_stats.st_mode)
print('\tCreated:', time.ctime(file_stats.st_ctime))
print('\tAccessed:', time.ctime(file_stats.st_atime))
print('\tModified:', time.ctime(file_stats.st_mtime))
os.mkdir('test')
print('SOURCE:')
displayFileStats(__file__)
copy2(__file__, 'testfile')
print('TARGET:')
displayFileStats(os.path.realpath(os.getcwd() + '/test/testfile'))
Renaming Files and Folders:
• The os.rename() is used to python rename file or directory. The rename() method
takes two arguments, the current filename and the new filename.
Syntax: os.rename(current_file_name, new_file_name)
Example:
import os
# Rename a file from test1.txt to test2.txt
os.rename( "test1.txt", "test2.txt" )
The following example shows the usage of rename() method.
# !/usr/bin/python3
import os, sys
os.chdir("d:\\tmp")
# listing directories
print ("The dir is: %s"%os.listdir(os.getcwd()))

4.47
Python Programming Modules, Working with Files and Exception Handling

# renaming directory ''tutorialsdir"


os.rename("python3","python2")
print ("Successfully renamed.")
# listing directories after renaming "python3"
print ("the dir is: %s" %os.listdir(os.getcwd()))
Output:
The dir is: [
'Applicationdocs.docx', 'book.zip', 'foo.txt',
'Java Multiple Inheritance.htm', 'Java Multiple Inheritance_files',
'java.ppt', 'Python3'
]
Successfully renamed.
the dir is: [
'Applicationdocs.docx', 'book.zip', 'foo.txt',
'Java Multiple Inheritance.htm', 'Java Multiple Inheritance_files',
'java.ppt', 'python2'
]
Example: For remaining files.
import os
print("***Contents of Present working directory***\n ",os.listdir())
os.rename("sample.txt","sample1.txt")
print("***Contents of Present working directory after rename***\n ",
os.listdir())
Output:
***Contents of Present working directory***
['DLLs', 'Doc', 'etc', 'file1.txt', 'include', 'Lib', 'libs',
'LICENSE.txt', 'mypkg', 'NEWS.txt', 'p1.py', 'p2.py', 'python.exe',
'python3.dll', 'python37.dll', 'pythonw.exe', 'sample.txt', 'Scripts',
'share', 'tcl', 'test.py', 'Tools', 'vcruntime140.dll', '_ _pycache_ _']
***Contents of Present working directory after rename***
['DLLs', 'Doc', 'etc', 'file1.txt', 'include', 'Lib', 'libs',
'LICENSE.txt', 'mypkg', 'NEWS.txt', 'p1.py', 'p2.py', 'python.exe',
'python3.dll', 'python37.dll', 'pythonw.exe', 'sample1.txt', 'Scripts',
'share', 'tcl', 'test.py', 'Tools', 'vcruntime140.dll', '_ _pycache_ _']
>>>
4.48
Python Programming Modules, Working with Files and Exception Handling

4.3.9 Splitting Pathnames


• A full file path is composed of following two components:
1. directory name
2. base name.
• Following methods splits a full pathname.
basename() Method:
• The os.path.basename() method takes a path name as an argument and returns the
leaf name of the specified path.
• For example, we have created a file called file1 in a directory called user1 under the
home directory.
• Now, we can use the basename() method to retrieve only the filename from the
path/home/user1/file1 by using the following code:
>>> import os
>>> os.path.basename('/home/user1/ifile1')
'file1'
dirname() Method:
• We can use the os.path.dirname() method to retrieve the directory
name from a path name.
• For example, the following code returns the directory name of the path
'/home/user1/file1':
>>> import os
>>> os.path.dirname('/home/user1/filel')
'/home/user1'
join( ) Method:
• The os.path.join() method joins two or more path components into a single path name.
This method takes the path components as arguments.
• The following example illustrates the use of the join() method:
>>>import os
>>>current_dir=os.getcwd()
>>>print current_dir
/home/
>>>join_dir=os.path.join(current_dir,'testfile')
>>>print join_dir
/home/testfile
• This example joins the path of the current working directory with the second
argument, testfile, and returns the joined path.
4.49
Python Programming Modules, Working with Files and Exception Handling

split() Method:
• The os.path.split() method splits a path, which is passed as an argument, into a
directory name and a base name and returns them as a tuple.
• The following example splits the joined path obtained in the previous example:
>>> import os.path
>>> os.path.split(join_dir)
('/home','testfile')
splitdrive() Method:
• The splitdrive() method is used to split the drive name and the path name of the
argument passed in the method.
• The following examples illustrates the use of the splitdrive() method in both Unix and
Windows versions:
o Unix Version:
>>> import os.path
>>> os.path.splitdrive('/home/testfile')
(' ', '/home/ testfile')
o Windows Version:
>>> import os.path
>>> os.path.splitdrive('c:/Python')
('c:', '/Python')
Table 4.2: The Information Methods of the os.path Module
Method Description
getsize(file_name) Returns the size of a file in bytes
getatime(file_name) Returns the time when a file was last accessed
getmtime(file_name) Returns the time when a file was last modified
splitext() Method:
• The splitext method separates the first name and the extension name of a filename.
Consider the following code:
>>> import os.path
>>> os.path.splitext('testfile.txt')
('testfile', '.txt')
• Let (name be a complete path to a file, say
/usr/home/hp1/scripting/python/intro/hw.py
4.50
Python Programming Modules, Working with Files and Exception Handling

• Occasionally you need to split such a filepath into the basename hw.py and the
directory name /usr/home/hpl/scripting/python/intro. In Python this is accomplished
by,
basename = os.path.basename(fname)
dirname = os.path.dirname(fname)
# or
dirname, basename = os.path.split(fname)
• The extension is extracted by the as path.split ext function,
root, extension = os.path.splitext(fname)
• Yielding '.py' for extension and the rest of f name for root.
• The extension without the leading dot is easily obtained by as,
path split ext (fname) 11] [1:].
• Changing some arbitrary extension of a file with name f to a new extension ext can be
done by,
newfile = os.path.splitext(f)[0] + ext
• Here is a specific example:
>>> f = '/some/path/case2.data_source'
>>> moviefile = os.path.basename(os.path.splitext(f)[0] + '.mpg')
>>> moviefile
'case2.mpg'

4.4 DIRECTORIES
• A directory or folder is nothing more than a location on a disk used for storing
information about files.
• All directories in Python are organized into a hierarchical structure that you can
imagine as a family tree.
• A directory or folder is a collection of files and sub directories.
• Python has the os module, which provides us with many useful methods to work with
directories (and files as well).
• All files are contained within various directories, and Python has no problem handling
these too.
• The os module has several methods that help us to create, remove, and change
directories.
• The os module contains the functions mkdir() for creating directories and chdir() for
moving to directories.
4.51
Python Programming Modules, Working with Files and Exception Handling

Directory

Directory Directory File

File File

File

Fig. 4.2: Directory Structure

4.4.1 Create New Directory


• A directory or folder is a collection of files and sub directories. Python has
the os module, which provides us with many useful methods to work with directories
(and files as well).
• We can make a new directory using the mkdir() method.
• This method takes in the path of the new directory. If the full path is not specified, the
new directory is created in the current working directory.
Syntax: os.mkdir(“newdir”)
Example:
>>> import os
>>> os.mkdir("testdir")

4.4.2 Get Current Directory


• We can get the present working directory using the getcwd() method.
• The getcwd() method returns the current working directory in the form of a string.
Syntax: os.getcwd()
Example:
>>> import os
>>> os.getcwd()
'C:\\Users\\ Meenakshi \\AppData\\Local\\Programs\\Python\\Python37-32'

4.4.3 Changing Directory


• We can change the current working directory using the chdir() method.
• The new path that we want to change must be supplied as a string to this method. We
can use both forward slash (/) and the backward slash (\) to separate path elements.
4.52
Python Programming Modules, Working with Files and Exception Handling

Syntax: os.chdir(“dirname”)
Example:
>>> import os
>>> os.getcwd()
'C:\\Users\\Meenakshi\\AppData\\Local\\Programs\\Python\\Python37'
>>> os.chdir("d:\IT")
>>> os.getcwd()
'd:\\IT'
>>>

4.4.4 List Directories and Files


• 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.
Example:
>>> os.listdir()
['DLLs', 'Doc', 'include', 'Lib', 'libs', 'LICENSE.txt', 'NEWS.txt',
'python.exe', 'python3.dll', 'python37.dll', 'pythonw.exe', 'Scripts',
'tcl', 'test.py', 'testdir', 'Tools', 'vcruntime140.dll']

4.4.5 Removing Directory


• The rmdir() method is used to remove directories in the current directory.
Syntax: os.rmdir(“dirname”)
Example:
>>> os.listdir()
['DLLs', 'Doc', 'etc', 'file1.txt', 'include', 'Lib', 'libs',
'LICENSE.txt', 'mydir', 'mypkg', 'NEWS.txt', 'p1.py', 'p2.py',
'python.exe', 'python3.dll', 'python37.dll', 'pythonw.exe',
'sample.txt', 'Scripts', 'share', 'tcl', 'test.py', 'Tools',
'vcruntime140.dll', '_ _pycache_ _']
>>> os.rmdir("mydir")
>>> os.listdir()
['DLLs', 'Doc', 'etc', 'file1.txt', 'include', 'Lib', 'libs',
'LICENSE.txt', 'mypkg', 'NEWS.txt', 'p1.py', 'p2.py', 'python.exe',
'python3.dll', 'python37.dll', 'pythonw.exe', 'sample.txt', 'Scripts',
'share', 'tcl', 'test.py', 'Tools', 'vcruntime140.dll', '_ _pycache_ _']
>>>
4.53
Python Programming Modules, Working with Files and Exception Handling

• If the directory is not empty then we will get the “The directory is not empty” error. To
remove a directory, first remove all the files inside it using os.remove() method.
>>> os.chdir("C:\\Users\\Meenakshi\\AppData\\Local\\Programs
\\Python\\Python37")
>>> os.rmdir("mydir1")
Traceback (most recent call last):
File "<pyshell#32>", line 1, in <module>
os.rmdir("mydir1")
OSError: [WinError 145] The directory is not empty: 'mydir1'
>>> os.chdir("C:\\Users\\Meenakshi\\AppData\\Local\\Programs
\\Python\\Python37\mydir1")
>>> os.listdir()
['sample.txt']
>>> os.remove("sample.txt")
>>> os.listdir()
[]
>>> os.chdir("C:\\Users\\Meenakshi\\AppData\\Local\\Programs
\\Python\\Python37")
>>> os.rmdir("mydir1")
>>>
• In order to remove a non-empty directory we can use the rmtree() method inside
the shutil module.
>>> import shutil
>>> shutil.rmtree('test')

PROGRAMS
Program 1: Program to create a simple file and write some content in it.
print("Enter 'x' for exit.");
filename = input("Enter file name to create and write content: ");
if filename == 'x':
exit();
else:
c = open(filename, "w");
print("\nThe file,",filename,"created successfully!");

4.54
Python Programming Modules, Working with Files and Exception Handling

print("Enter sentences to write on the file: ");


sent1 = input();
c.write(sent1);
c.close();
print("\nContent successfully placed inside the file.!!");
Output:
Enter 'x' for exit.
Enter file name to create and write content: file1
The file, file1 created successfully!
Enter sentences to write on the file:
good morning
Content successfully placed inside the file.!!
>>>

Program 2: Program to open a file in write mode and append some content at the end of a
file.
print("Enter 'x' for exit.");
filename = input("Enter file name to append content: ");
if filename == 'x':
exit();
else:
c=open(filename, "a+");
print("Enter sentences to append on the file: ");
sent1=input();
c.write("\n");
c.write(sent1);
c.close();
print("\nContent appended to file.!!");

Output:
Enter 'x' for exit.
Enter file name to append content: file1
Enter sentences to append on the file:
good afternoon
Content appended to file.!!

4.55
Python Programming Modules, Working with Files and Exception Handling

Program 3: Program to open a file in read mode and print number of occurrences of
characters ‘a’.
fname = input("Enter file name: ")
l=input("Enter letter to be searched:")
k = 0
with open(fname, 'r') as f:
for line in f:
words = line.split()
for i in words:
for letter in i:
if(letter==l):
k=k+1
print("Occurrences of the letter:")
print(k)
Output:
Enter file name: file1
Enter letter to be searched:o
Occurrences of the letter:
7
Program 4: Program to read the contents of a file.
a=str(input("Enter the name of the file with .txt extension:"))
file2=open(a,'r')
line=file2.readline()
while(line!=""):
print(line)
line=file2.readline()
file2.close()
Output:
Enter the name of the file with .txt extension: data1.txt
Program 5: Program to count the number of words and lines in a text file.
fname = input("Enter file name: ")
num_words = 0
lines=0
with open(fname, 'r') as f:
for line in f:
words = line.split()
num_words += len(words)
lines += 1
print("Number of words:")
4.56
Python Programming Modules, Working with Files and Exception Handling

print(num_words)
print("Number of lines:")
print(num_lines)
read.txt
Contents of file:
Hello world
Output:
Enter file name: data1.txt
Number of words:
2
Number of lines:
1
Program 6: Program to count the occurrences of a word in a text file.
fname = input("Enter file name: ")
word=input("Enter word to be searched:")
k = 0
with open(fname, 'r') as f:
for line in f:
words = line.split()
for i in words:
if(i==word):
k=k+1
print("Occurrences of the word:")
print(k)
read.txt
Let Contents of file:
hello world hello
hello
Output:
Enter file name: test.txt
Enter word to be searched:hello
Occurrences of the word:
3
Program 7: Program to copy the contents of one file into another.
with open("test.txt") as f:
with open("out.txt", "w") as f1:
for line in f:
f1.write(line)

4.57
Python Programming Modules, Working with Files and Exception Handling

Program 8: Program to reads a text file and counts the number of times a certain letter
appears in the text file.
fname = input("Enter file name: ")
l=input("Enter letter to be searched:")
k = 0
with open(fname, 'r') as f:
for line in f:
words = line.split()
for i in words:
for letter in i:
if(letter==l):
k=k+1
print("Occurrences of the letter:")
print(k)
read.txt:
Case 1:
Contents of file:
hello world hello
hello
Output:
Enter file name: out.txt
Enter letter to be searched:o
Occurrences of the letter:
5
Program 9: Program to read a file and capitalize the first letter of every word in the file.
fname = input("Enter file name: ")
with open(fname, 'r') as f:
for line in f:
l=line.title()
print(l)
read.txt:
Case 1:
Contents of file:
hello world
hello
Output:
Enter file name: read.txt
Hello World
Hello
4.58
Python Programming Modules, Working with Files and Exception Handling

Program 10: Program to read the contents of a file in reverse order.


filename=input("Enter file name: ")
for line in reversed(list(open(filename))):
print(line.rstrip())
read.txt:
Contents of file:
hello world
hello
Output:
Enter file name: read.txt
hello
hello word

4.5 REGULAR EXPRESSIONS


• A Regular Expression or RegEx or REs is a sequence of characters that forms a search
pattern. RegEx can be used to check if a string contains the specified search pattern.
• The regular expressions can be defined as the sequence of characters which are used
to search for a pattern in a string.
• The module ‘re’ provides the support to use RegEx in the python program.
The ‘re’ module must be imported to use the RegEx functionalities in python like
import re.
• The ‘re’ module included with Python primarily used for string searching and
manipulation.
• When we have imported the re module, we can start using regular expressions.
The re module offers a set of functions like findall(), search(), split(), match() etc. that
allows us to search a string for a match.

4.5.1 Concept of Regular Expression


• A regular expression is a special sequence of characters that helps us match or find
other strings or sets of strings, using a specialized syntax held in a pattern.
• Regular expressions are widely used in UNIX world. The Python module re provides
full support for Perl-like regular expressions in Python.
• The re module raises the exception re.error if an error occurs while compiling or
using a regular expression.
• We would cover two important functions, which would be used to handle regular
expressions. But a small thing first: There are various characters, which would have
special meaning when they are used in regular expression.
• To avoid any confusion while dealing with regular expressions, we would use Raw
Strings as r'expression'.
4.59
Python Programming Modules, Working with Files and Exception Handling

• Regular Expressions are a sequence of characters used to check whether a pattern


exists in a given text (string) or not.

4.5.2 Types of Regular Expression


• A regular expression is a special sequence of characters that helps we match or find
other strings or sets of strings, using a specialized syntax held in a pattern.
• There are two types of regular expression namely, Basic and Extended expressions.
These regular expressions are two variations on the syntax of the specified pattern.
1. Basic Regular Expression: The simplest regular expression consists of the exact
characters of the string that it is intended to match. The regular language defined
by the expression consists of only that one string. Upper and lower case letters are
regarded as different symbols. In basic regular expressions the meta-characters
‘?’, ‘+’, ‘{’, ‘|’, ‘(’, and ‘)’ lose their special meaning; instead use the backslashed
versions ‘\?’, ‘\+’, ‘\{’, ‘\|’, ‘\(’, and ‘\)’.
2. Extended Regular Expression: An extended regular expression specifies a set of
strings to be matched. The expression contains both text characters and operator
characters. Text characters match the corresponding characters in the strings
being compared. Operator characters specify repetitions, choices, and other
features. An extended regular expression ordinary character, a special character
preceded by a <backslash,> or a <period> shall match a single character.
• The real power of RegEx matching in Python emerges when it contains special
characters called metacharacters.
• Metacharacters are special characters that affect how the regular expression finds the
patterns and are mostly used to define the pattern of search or manipulation.
• In a regex, a set of characters specified in square brackets ([]) makes up a character
class. This metacharacter sequence matches any single character that is in the class.

Metacharacter Description
^ Matches the start of the string.
. Matches a single character, except a newline.
But when used inside square brackets, a dot is matched.
[] A bracket expression matches a single character from the ones
inside it.
[abc] matches ‘a’, ‘b’, and ‘c’.
[a-z] matches characters from ‘a’ to ‘z’.
[a-cx-z] matches ‘a’, ’b’, ’c’, ’x’, ’y’, and ‘z’.
[^ ] Matches a single character from those except the ones mentioned in
the brackets[^abc] matches all characters except ‘a’, ‘b’ and ‘c’.
contd. …
4.60
Python Programming Modules, Working with Files and Exception Handling

() Parentheses define a marked subexpression, also called a block, or a


capturing group.
\t, \n, \r, \f Tab, newline, return, form feed.
* Matches the preceding character zero or more times.
ab*c matches ‘ac’, ‘abc’, ‘abbc’, and so on.
[ab]* matches ‘’, ‘a’, ‘b’, ‘ab’, ‘ba’, ‘aba’, and so on.
(ab)* matches ‘’, ‘ab’, ‘abab’, ‘ababab’, and so on.
{m,n} Matches the preceding character minimum m times, and maximum
n times.
a{2,4} matches ‘aa’, ‘aaa’, and ‘aaaa’.
{m} Matches the preceding character exactly m times.
? Matches the preceding character zero or one times.
ab?c matches ‘ac’ or ‘abc’.
+ Matches the preceding character one or one times.
ab+c matches ‘abc’, ‘abbc’, ‘abbbc’, and so on, but not ‘ac’.
| The choice operator matches either the expression before it, or the
one after
abc|def matches ‘abc’ or ‘def’
• There are some basic predefined character classes, which are represented by the
special sequence. The special sequence consists of alphabetic characters lead by \
(backlash).
• Each special sequence has a unique meaning that helps us find or match other strings
or sets of strings using a specialized syntax present in a pattern.
• A special sequence is a \ followed by one of the characters in the list below, and has a
special meaning. RegEx special sequences and their meanings are given in the
following table:
Character Description Example
\A Returns a match if the specified characters are at the "\AThe"
beginning of the string.
\b Returns a match where the specified characters are at r"\bain"
the beginning or at the end of a word r"ain\b"
(the "r" in the beginning is making sure that the string is
being treated as a "raw string").
\B Returns a match where the specified characters are r"\Bain"
present, but NOT at the beginning (or at the end) of a r"ain\B"
word
(the "r" in the beginning is making sure that the string is
being treated as a "raw string").
contd. …
4.61
Python Programming Modules, Working with Files and Exception Handling

\d Returns a match where the string contains digits "\d"


(numbers from 0-9).
\D Returns a match where the string DOES NOT contain "\D"
digits.
\s Returns a match where the string contains a white space "\s"
character.
\S Returns a match where the string DOES NOT contain a "\S"
white space character.
\w Returns a match where the string contains any word "\w"
characters (characters from a to Z, digits from 0-9, and
the underscore _ character).
\W Returns a match where the string DOES NOT contain any "\W"
word characters.
\Z Returns a match if the specified characters are at the end "Spain\Z"
of the string.
• Character sets are a predefined range of characters enclosed by a square bracket.
With the use of sets, we can match only one out of several characters.
• A set is a set of characters inside a pair of square brackets [] with a special meaning.
Set Description
[arn] Returns a match where one of the specified characters (a, r, or n) are
present.
[a-n] Returns a match for any lower case character, alphabetically
between a and n.
[^arn] Returns a match for any character EXCEPT a, r, and n.
[0123] Returns a match where any of the specified digits (0, 1, 2, or 3) are
present.
[0-9] Returns a match for any digit between 0 and 9.
[0-5][0-9] Returns a match for any two-digit numbers from 00 and 59.
[a-zA-Z] Returns a match for any character alphabetically between a and z, lower
case OR upper case.
[+] In sets, +, *, ., |, (), $,{} has no special meaning, so [+] means: return a
match for any + character in the string.

4.5.3 Methods of re Package


• The "re" package provides methods such as re.match(), re.search() and re.findall().
Using match() Function:
• A Match Object is an object containing information about the search and the result.
4.62

You might also like