You are on page 1of 18

GE1105 PROBLEM SOLVING AND PYTHON PROGRAMMING UNIT - 5

UNIT 5
FILES, MODULES, PACKAGES
Files and exception: Concept of Files, Text Files; File opening in various modes and closing of a
file, Format Operators, Reading from a file, Writing onto a file, File functions-open(), close(),
read(), readline(), readlines(),write(), writelines(),tell(),seek(), Command Line arguments.
Errors and exceptions, handling exceptions, modules, packages; introduction to numpy,
matplotlib. Illustrative programs: word count, copy file.

FILES
❖ A file is a named location on a disk to store related information.
❖ It is used to permanently store data in non-volatile memory (e.g. hard disk).
❖ Since random access memory (RAM) is volatile and loses its data when a computer is turned
off, we use files for future use of the data.
❖ 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 to the file are freed.
Hence, in Python, a file operation takes place in the following order.
1. Open a file
2. Read or write (perform the operation)
3. Close the file

1. Opening a file
Python has a built-in function open() to open a file. This function returns a file object, also called a
handle, as it is used to read or modify the file accordingly.
>>> f = open("test.txt") # open file in current directory
>>> f = open("C:/Python33/README.txt") # specifying full path
We can specify the mode while opening a file. In mode, we specify whether we want to read 'r', write
'w', or append 'a' to the file. We also specify if we want to open the file in text mode or binary mode.
The default is reading in text mode. In this mode, we get strings when reading from the file. On the
other hand, the binary mode returns bytes and this is the mode to be used when dealing with non-text
files like image or exe files.
Python File Modes
Mode: Description
❖ 'r': Open a file for reading. (default)
❖ 'w': Open a file for writing. Creates a new file if it does not exist or truncates the file if it exists.
❖ 'x': Open a file for exclusive creation. If the file already exists, the operation fails.
❖ 'a': Open for appending at the end of the file without truncating it. Creates a new file if it does
not exist.
P a g e 1 | 18
St. Joseph’s College of Engineering
GE1105 PROBLEM SOLVING AND PYTHON PROGRAMMING UNIT - 5

❖ 't': Open in text mode. (default)


❖ 'b': Open in binary mode.
❖ '+': Open a file for updating (reading and writing)
f = open("test.txt") # equivalent to 'r' or 'rt'
f = open("test.txt",'w') # write in text mode
f = open("img.bmp",'r+b') # read and write in binary mode
Hence, when working with files in text mode, it is highly recommended to specify the encoding type.
f = open("test.txt",mode = 'r',encoding = 'utf-8')

2. Closing a File
❖ When we are done with operations on the file, we need to properly close it.
❖ Closing a file will free up the resources that were tied to the file and is done using the close()
method.
❖ Python has a garbage collector to clean up unreferenced objects but, we must not rely on it to
close the file.
f = open("test.txt",encoding = 'utf-8')
# perform file operations
f.close()
This method is not entirely safe. If an exception occurs when we are performing some operation with
the file, the code exits without closing the file. A safer way is to use a try...finally block.
try:
f= open("test.txt",encoding = 'utf-8')
# perform file operations
finally:
f.close()
This way, we are guaranteed that the file is properly closed even if an exception is raised, causing the
program flow to stop.
The best way to do this is by using the statement. This ensures that the file is closed when the block
inside with is exited.
We don't need to explicitly call the close() method. It is done internally.
with open("test.txt",encoding = 'utf-8') as f:
# perform file operations

P a g e 2 | 18
St. Joseph’s College of Engineering
GE1105 PROBLEM SOLVING AND PYTHON PROGRAMMING UNIT - 5

3. Reading and writing


A text file is a sequence of characters stored on a permanent medium like a hard drive, flash memory,
or CD-ROM.
To write a file, you have to open it with mode 'w' as a second parameter:
>>> fout = open('output.txt', 'w')
>>> print fout
<open file 'output.txt', mode 'w' at 0xb7eb2410>
If the file already exists, opening it in write mode clears out the old data and starts fresh, so be careful!
If the file doesn’t exist, a new one is created.
The write method puts data into the file.
>>> line1 = "This here's the wattle,\n"
>>>fout.write(line1)
Again, the file object keeps track of where it is, so if you call to write again, it adds the new data to the
end.
>>> line2 = "the emblem of our land.\n"
>>> fout.write(line2)
When you are done writing, you have to close the file.
>>> fout.close()
Here are some of the functions in Python that allow you to read and write files:
❖ read(): This function reads the entire file and returns a string
❖ readline(): This function reads lines from that file and returns them as a string. It fetches the
line n if it is been called the nth time.
❖ readlines(): This function returns a list where each element is a single line of that file.
❖ write(): This function writes a fixed sequence of characters to a file.
❖ writelines(): This function writes a list of strings.
❖ append(): This function appends a string to the file instead of overwriting the file.
❖ tell(): Returns the current file position in a file stream.
❖ seek(): Change the current file position with the
4. Format operator
The argument of write has to be a string, so if we want to put other values in a file, we have to convert
them to strings.
The easiest way to do that is with str:
>>> x = 52
>>> fout.write(str(x))

P a g e 3 | 18
St. Joseph’s College of Engineering
GE1105 PROBLEM SOLVING AND PYTHON PROGRAMMING UNIT - 5

❖ An alternative is to use the format operator, %. When applied to integers, % is the modulus
operator. But when the first operand is a string, % is the format operator.
❖ The first operand is the format string, which contains one or more format sequences, which
specify how the second operand is formatted. The result is a string.
For example, the format sequence '%d' means that the second operand should be formatted as an integer
(d stands for “decimal”):
>>> camels = 42
>>> '%d' % camels
'42'
The result is the string '42', which is not to be confused with the integer value 42. A format sequence
can appear anywhere in the string, so you can embed a value in a sentence:
>>> camels = 42
>>> 'I have spotted %d camels.' % camels
'I have spotted 42 camels.'
❖ If there is more than one format sequence in the string, the second argument has to be a tuple.
❖ Each format sequence is matched with an element of the tuple, in order.
The following example uses '%d' to format an integer, '%g' to format a floating-point number, and '%s'
to format a string:
>>> 'In %d years I have spotted %g %s.' % (3, 0.1, 'camels')
'In 3 years I have spotted 0.1 camels.'
The number of elements in the tuple has to match the number of format sequences in the string. Also,
the types of elements have to match the format sequences:
>>> '%d %d %d' % (1, 2)
TypeError: not enough arguments for format string
>>> '%d' % 'dollars'
TypeError: illegal argument type for built-in operation

PYTHON COMMAND LINE ARGUMENTS


Python Command Line Arguments provide a convenient way to accept some information at the
command line while running the program. The arguments that are given after the name of the Python
script are known as Command Line Arguments and they are used to pass some information to the
program.
For example -
$ python script.py arg1 arg2 arg3

P a g e 4 | 18
St. Joseph’s College of Engineering
GE1105 PROBLEM SOLVING AND PYTHON PROGRAMMING UNIT - 5

Here Python script name is script.py and the rest of the three arguments - arg1 arg2 arg3 is command
line arguments for the program. There are following three Python modules that are helpful in parsing
and managing the command line arguments:
1. sys module
2. getopt module
3. argparse module
1. sys module - System-specific parameters
The Python sys module provides access to any command-line arguments via the sys.argv. This serves
two purposes −
• sys.argv is the list of command-line arguments.
• len(sys.argv) is the number of command-line arguments.
Here sys.argv[0] is the program ie. script name.
Example
Consider the following script test.py −
import sys

print 'Number of arguments:', len(sys.argv), 'arguments.'


print 'Argument List:', str(sys.argv)
Now run the above script as below
$ python test.py arg1 arg2 arg3
This produces the following result −
Number of arguments: 4 arguments.
Argument List: ['test.py', 'arg1', 'arg2', 'arg3']
As mentioned above, the first argument is always the script name and it is also counted in a number
of arguments.

2. Parsing Command-Line Arguments


Python provided a getopt module that helps you parse command-line options and arguments. This
module provides two functions and an exception to enable command-line argument parsing.
getopt.getopt method
This method parses command line options and parameter lists. The following is a simple syntax for
this method −

P a g e 5 | 18
St. Joseph’s College of Engineering
GE1105 PROBLEM SOLVING AND PYTHON PROGRAMMING UNIT - 5

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


Here is the detail of the parameters −
• args − This is the argument list to be parsed.
• options − This is the string of option letters that the script wants to recognize, with options
that require an argument that should be followed by a colon (:).
• long_options − This is an optional parameter and if specified, must be a list of strings with
the names of the long options, which should be supported. Long options, which require an
argument should be followed by an equal sign ('='). To accept only long options, options
should be an empty string.
This method getopt.getopt() returns a value consisting of two elements: the first is a list of (option,
value) pairs. The second is the list of program arguments left after the option list was stripped. Each
option-and-value pair returned has the option as its first element, prefixed with a hyphen for short
options (e.g., '-x') or two hyphens for long options (e.g., '--long option).
Example
Following is a Python program that takes three arguments at the command line:
1. The first command line argument is -h which will be used to display the usage help of the
program.
2. The second argument is either -i or --ifile which we are considering as an input file.
3. The third argument is either -o or --ofile which we are considering as an output file.
4.
3. Python argparse Module
Python argparse module makes it easy to write user-friendly command-line interfaces. The program
defines what arguments it requires, and argparse will figure out how to parse those out of sys.argv.
The argparse module also automatically generates help and usage messages. The module will also
issue errors when users give the program invalid arguments.

EXCEPTION
Python (interpreter) raises exceptions when it encounters errors. Error caused by not following the
proper structure (syntax) of the language is called syntax error or parsing error.
>>> if a < 3
File "<interactive input>", line 1
if a < 3
SyntaxError: invalid syntax
❖ Errors can also occur at runtime and these are called exceptions.

P a g e 6 | 18
St. Joseph’s College of Engineering
GE1105 PROBLEM SOLVING AND PYTHON PROGRAMMING UNIT - 5

❖ They occur, for example, when a file we try to open does not exist (FileNotFoundError),
dividing a number by zero (ZeroDivisionError), the module we try to import is not found
(ImportError), etc.
❖ Whenever these types of runtime errors occur, Python creates an exception object.
❖ If not handled properly, it prints a traceback to that error along with some details about why
that error occurred.
>>> 1 / 0
Traceback (most recent call last):
File "<string>", line 301, in runcode
File "<interactive input>", line 1, in <module>
ZeroDivisionError: division by zero

>>> open("imaginary.txt")
Traceback (most recent call last):
File "<string>", line 301, in runcode
File "<interactive input>", line 1, in <module>
FileNotFoundError: [Errno 2] No such file or directory: 'imaginary.txt'

1. Python Built-in Exceptions


Illegal operations can raise exceptions. There are plenty of built-in exceptions in Python that are raised
when corresponding errors occur. We can view all the built-in exceptions using the local() built-in
functions as follows.
>>> locals()['__builtins__']
This will return us a dictionary of built-in exceptions, functions, and attributes. Some of the common
built-in exceptions in Python programming along with the error that causes them are tabulated below.
Exceptions: Cause of Error
❖ AssertionError: Raised when assert statement fails.
❖ AttributeError: Raised when attribute assignment or reference fails.
❖ EOFError: Raised when the input() functions hit the end-of-file condition.
❖ FloatingPointError: Raised when a floating point operation fails.
❖ GeneratorExit: Raise when a generator's close() method is called.
❖ ImportError: Raised when the imported module is not found.
❖ IndexError: Raised when the index of a sequence is out of range.
❖ KeyError: Raised when a key is not found in a dictionary.
❖ KeyboardInterrupt: Raised when the user hits the interrupt key (Ctrl+c or delete).
❖ MemoryError: Raised when an operation runs out of memory.
❖ NameError: Raised when a variable is not found in the local or global scope.
P a g e 7 | 18
St. Joseph’s College of Engineering
GE1105 PROBLEM SOLVING AND PYTHON PROGRAMMING UNIT - 5

❖ NotImplementedError: Raised by abstract methods.


❖ OSError: Raised when system operation causes the system-related error.
❖ OverflowError: Raised when the result of an arithmetic operation is too large to be
represented.
❖ ReferenceError: Raised when a weak reference proxy is used to access a garbage-collected
referent.
❖ RuntimeError: Raised when an error does not fall under any other category.
❖ StopIteration: Raised by the next() function to indicate that there is no further item to be
returned by the iterator.
❖ SyntaxError: Raised by the parser when a syntax error is encountered.
❖ IndentationError: Raised when there is incorrect indentation.
❖ TabError: Raised when indentation consists of inconsistent tabs and spaces.
❖ SystemError: Raised when the interpreter detects an internal error.
❖ SystemExit: Raised by sys. exit() function.
❖ TypeError: Raised when a function or operation is applied to an object of the incorrect type.
❖ UnboundLocalError: Raised when a reference is made to a local variable in a function or
method, but no value has been bound to that variable.
❖ UnicodeError: Raised when a Unicode-related encoding or decoding error occurs.
❖ UnicodeEncodeError: Raised when a Unicode-related error occurs during encoding.
❖ UnicodeDecodeError: Raised when a Unicode-related error occurs during decoding.
❖ UnicodeTranslateError: Raised when a Unicode-related error occurs during translating.
❖ ValueError: Raised when a function gets an argument of correct type but improper value.
❖ ZeroDivisionError: Raised when the second operand of division or modulo operation is zero.
We can handle these built-in and user-defined exceptions in Python using try, except, and finally
statements.

2. Python Exception Handling


❖ Python has many built-in exceptions which force your program to output an error when
something in it goes wrong.
❖ When these exceptions occur, it causes the current process to stop and passes it to the calling
process until it is handled. If not handled, our program will crash.
❖ For example, if function A calls function B which in turn calls function C and an exception
occurs in function C. If it is not handled in C, the exception passes to B and then to A.
❖ If never handled, an error message is spit out and our program comes to a sudden, unexpected
halt.
2.1 Catching Exceptions in Python
❖ In Python, exceptions can be handled using a try statement.
❖ A critical operation that can raise an exception is placed inside the try clause and the code that
handles the exception is written in except clause.
❖ It is up to us, what operations we perform once we have caught the exception.
Example: Catch Specific Error Type
try:

P a g e 8 | 18
St. Joseph’s College of Engineering
GE1105 PROBLEM SOLVING AND PYTHON PROGRAMMING UNIT - 5

a=5
b='0'
print (a+b)
except TypeError:
print('Unsupported operation')
print ("Out of try except blocks")
Output
Unsupported operation
Out of try except blocks
2.2 try...finally
❖ The try statement in Python can have an optional finally clause. This clause is executed no
matter what and is generally used to release external resources.
❖ For example, we may be connected to a remote data center through the network or working
with a file, or working with a Graphical User Interface (GUI).
❖ In all these circumstances, we must clean up the resource once used, whether it was successful
or not. These actions (closing a file, GUI, or disconnecting from the network) are performed in
the finally clause to guarantee execution.
Syntax and Examples for various try and finally blocks
Syntax: Example: try...except blocks try:
try : a=5
#statements in the try block b='0'
except : print(a/b)
#executed when an error in the try except:
block
print('Some error occurred.')
print("Out of try except blocks.")

Output
Some error occurred.
Out of try except blocks.
Syntax: Example: Multiple except Blocks
try :
try:
#statements in the try block
a=5
except1 :
b=0

P a g e 9 | 18
St. Joseph’s College of Engineering
GE1105 PROBLEM SOLVING AND PYTHON PROGRAMMING UNIT - 5

#executed when an error in the try print (a/b)


block
except TypeError:
except2 :
print('Unsupported operation')
#executed when an error in the try
except ZeroDivisionError:
block
print ('Division by zero not allowed')
…..
print ('Out of try except blocks')
except n:
Output
#executed when an error in the try
block Division by zero not allowed
Out of try except blocks
Syntax: else and finally Example: try, except, else, finally blocks
try: try:
#statements in try block print('try block')
except: x=int(input('Enter a number: '))
#executed when error in try block y=int(input('Enter another number: '))
else: z=x/y
except ZeroDivisionError:
#executed if try block is error-free
print("except ZeroDivisionError block")
finally:
print("Division by 0 not accepted")
#executed irrespective of exception else:
occured or not print("else block")
print("Division = ", z)
finally:
print("finally block")
x=0
y=0
print ("Out of try, except, else and finally
blocks." )

Output
try block
Enter a number: 10
Enter another number: 2
else block
Division = 5.0
finally block
Out of try, except, else and finally blocks.

P a g e 10 | 18
St. Joseph’s College of Engineering
GE1105 PROBLEM SOLVING AND PYTHON PROGRAMMING UNIT - 5

2.3 How to Raise an exception


If a condition does not meet our criteria but is correct according to the Python interpreter, we can
intentionally raise an exception using the raise keyword. We can use a customized exception in
conjunction with the statement.
If we wish to use raise to generate an exception when a given condition happens, we may do so as
follows:
#Python code to show how to raise an exception in Python
num = [3, 4, 5, 7]
if len(num) > 3:
raise Exception( f"Length of the given list must be less than or equal to 3 but is {l
en(num)}" )

MODULES
Any file that contains Python code can be imported as a module. For example, suppose you have a file
named wc.py with the following code:
def linecount(filename):
count = 0
for line in open(filename):
count += 1
return count
print linecount('wc.py')
If you run this program, it reads itself and prints the number of lines in the file, which is 7.
You can also import it like this:
>>> import wc
7
Now you have a module object wc:
>>> print wc
<module 'wc' from 'wc.py'>
>>> wc.linecount('wc.py')
7

P a g e 11 | 18
St. Joseph’s College of Engineering
GE1105 PROBLEM SOLVING AND PYTHON PROGRAMMING UNIT - 5

So that’s how you write modules in Python.


The only problem with this example is that when you import the module it executes the test code at
the bottom. Normally when you import a module, it defines new functions but it doesn’t execute them.
Programs that will be imported as modules often use the following idiom: if __name__ ==
'__main__':
print linecount('wc.py')
__name__ is a built-in variable that is set when the program starts. If the program is running as a script,
__name__ has the value __main__; in that case, the test code is executed. Otherwise, if the module is
being imported, the test code is skipped.
Example:
# import module import calendar

yy= 2017
mm = 8

# To ask month and year from the user


# yy = int(input("Enter year: "))
# mm = int(input("Enter month: "))

display the calendar


print(calendar.month(yy, mm))

PACKAGE
A package is a collection of modules. A Python package can have sub-packages and modules.
A directory must contain a file named __init__.py for Python to consider it as a package. This file can
be left empty but we generally place the initialization code for that package in this file.
Here is an example. Suppose we are developing a game, one possible organization of packages and
modules could be as shown in the figure below.

P a g e 12 | 18
St. Joseph’s College of Engineering
GE1105 PROBLEM SOLVING AND PYTHON PROGRAMMING UNIT - 5

1. Importing module from a package


We can import modules from packages using the dot (.) operator.
For example, if want to import the start module in the above example, it is done as follows. import
Game.Level.start
Now if this module contains a function named select_difficulty(), we must use the full name to
reference it.
Game.Level.start.select_difficulty(2)
If this construct seems lengthy, we can import the module without the package prefix as follows.
from Game.Level import start
We can now call the function simply as follows.
start.select_difficulty(2)
Yet another way of importing just the required function (or class or variable) form a module within a
package would be as follows.
from Game.Level.start import select_difficulty
Now we can directly call this function.
select_difficulty(2)

❖ Although easier, this method is not recommended. Using the full namespace avoids confusion
and prevents two same identifier names from colliding.
❖ While importing packages, Python looks in the list of directories defined in sys.path, similar
to the module search path.
P a g e 13 | 18
St. Joseph’s College of Engineering
GE1105 PROBLEM SOLVING AND PYTHON PROGRAMMING UNIT - 5

INTRODUCTION TO NUMPY, MATPLOTLIB


NumPy package contains numpy.linalg module that provides all the functionality required for linear
algebra. Some of the important functions in this module are described in the following table.
Matplotlib is a plotting library for Python. It is used along with NumPy to provide an environment that
is an effective open-source alternative for MatLab. It can also be used with graphics toolkits like PyQt
and wxPython.
The matplotlib module was first written by John D. Hunter. Since 2012, Michael Droettboom is the
principal developer. Currently, Matplotlib ver. 1.5.1 is the stable version available. The package is
available in binary distribution as well as in the source code form on www.matplotlib.org.
Conventionally, the package is imported into the Python script by adding the following statement −
from matplotlib import pyplot as plt
Here pyplot() is the most important function in matplotlib library, which is used to plot 2D data. The
following script plots the equation y = 2x + 5
Example
import numpy as np
from matplotlib import pyplot as plt

x = np.arange(1,11)
y=2*x+5
plt.title("Matplotlib demo")
plt.xlabel("x axis caption")
plt.ylabel("y axis caption")
plt.plot(x,y)
plt.show()
A ndarray object x is created from np.arange() function as the values on the x axis. The corresponding
values on the y-axis are stored in another ndarray object y. These values are plotted using the
plot() function of pyplot submodule of matplotlib package.
The graphical representation is displayed by show() function.
The above code should produce the following output −

P a g e 14 | 18
St. Joseph’s College of Engineering
GE1105 PROBLEM SOLVING AND PYTHON PROGRAMMING UNIT - 5

ILLUSTRATION PROGRAM

1. Word Count

import sys
file=open("/Python27/note.txt","r+")
wordcount={}
for word in file.read().split():
if word not in wordcount:
wordcount[word] = 1
else:
wordcount[word] += 1
file.close();
print ("%-30s %s " %('Words in the File' , 'Count'))
for key in wordcount.keys():
print ("%-30s %d " %(key , wordcount[key]))

P a g e 15 | 18
St. Joseph’s College of Engineering
GE1105 PROBLEM SOLVING AND PYTHON PROGRAMMING UNIT - 5

EXAMPLE PYTHON PROGRAMS ON FILES, MODULES, PACKAGES

1. Write a function that copies a file reading and writing up to 50 characters at a time.

def copyFile(oldFile, newFile):


f1 = open(oldFile, "r")
f2 = open(newFile, "w")
while True:
text = f1.read(50)
if text == "":
break
f2.write(text)
f1.close()
f2.close()
return

2. (a) Write a program to perform exception handling.

def exists(filename):
try:
f = open(filename)
f.close()
return True
except IOError:
return False

P a g e 16 | 18
St. Joseph’s College of Engineering
GE1105 PROBLEM SOLVING AND PYTHON PROGRAMMING UNIT - 5

2. (b) Write a Python program to handle multiple exceptions.


try:
x= float(raw_input("Your number: "))
inverse = 1.0 / x
except ValueError:
print "You should have given either an int or a float"
except ZeroDivisionError:
print "Infinity"

3. Write a python program to count number of lines, words and characters in a text file. def
wordCount():
cl=0
cw=0
cc=0
f=open("ex88.txt","r") for line in f:
words=line.split()
cl +=1
cw +=len(words)
cc+=len(line)
print('No. of lines:',cl)
print('No. of words:',cw)
print('No. of characters:',cc)
f.close()

4. Write a Python program to illustrate the use of command-line arguments.

import sys
def inputCmd():
print ("Name of the script:", sys.argv[0])
print ("Number of arguments:", len(sys.argv))

P a g e 17 | 18
St. Joseph’s College of Engineering
GE1105 PROBLEM SOLVING AND PYTHON PROGRAMMING UNIT - 5

print ("The arguments are:", str(sys.argv))

5. Mention the commands and their syntax for the following: get current directory, changing
directory, list, directories and files, make a new directory, renaming and removing directory.

(a) Get current directory: getcwd()


Syntax : import os
os.getcwd()
(b) Changing directory: chdir()
Syntax: os.chdir(‘C:\\Users’)
os.getcwd()
(c) List directories and files: listdir()
Syntax: os.listdir()
(d) Making a new directory: mkdir()
Syntax: os.mkdir(‘Newdir’)
(e) Renaming a directory: rename()
os.rename(‘Newdir’,’Newname’)
os.listdir()
(f) Removing a directory: remove()
os.remove(‘NewName’)

P a g e 18 | 18
St. Joseph’s College of Engineering

You might also like