0% found this document useful (0 votes)
21 views20 pages

GE3151 Unit V

This document provides an overview of file handling, exceptions, and command line arguments in Python. It covers file operations such as opening, reading, writing, and closing files, as well as handling errors and exceptions using try-except blocks. Additionally, it discusses built-in exceptions and how to manage command line arguments with the sys module.

Uploaded by

SHEEBA M C CSE
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)
21 views20 pages

GE3151 Unit V

This document provides an overview of file handling, exceptions, and command line arguments in Python. It covers file operations such as opening, reading, writing, and closing files, as well as handling errors and exceptions using try-except blocks. Additionally, it discusses built-in exceptions and how to manage command line arguments with the sys module.

Uploaded by

SHEEBA M C CSE
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/ 20

UNIT V

FILES, MODULES, PACKAGES

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

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.

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.
1. Open a file
2. Read or write (perform operation)
3. Close the file

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, 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.
'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')

Closing a File
When we are done with operations to the file, we need to properly close it.

Closing a file will free up the resources that were tied with 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 program flow to stop. The best way to do this is using the with 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

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.
• read() : This function reads the entire file and returns a string
• readline() : This function reads lines from that file and returns as a string. It fetches the
line n, if it is been called nth time.
• readlines() : This function returns a list where each element is single line of that file.
• write() : This function writes a fixed sequence of characters to a file.
• writelines() : This function writes a list of string.
• append() : This function append string to the file instead of overwriting the file.

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 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()

Format operator

The argument of write has to be a string, so if we want to put other values in a file, we have to
convert them to strings. The easiest way to do that is with str:

>>> x = 52
>>> fout.write(str(x))

An alternative is to use the format operator, %. When applied to integers, % is the modulus
operator. But when the first operand is a string, % is the format operator.

The first operand is the format string, which contains one or more format sequences, which
specify how the second operand is formatted. The result is a string.

For example, the format sequence '%d' means that the second operand should be formatted as
an integer (d stands for “decimal”):
>>> camels = 42
>>> '%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 the 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

Filenames and paths

Files are organized into directories (also called “folders”). Every running program has a
“current directory,” which is the default directory for most operations. For example, when you
open a file for reading, Python looks for it in the current directory.

The os module provides functions for working with files and directories (“os” stands for
“operating system”). os.getcwd returns the name of the current directory:

>>> import os
>>> cwd = os.getcwd()
>>> print cwd
/home/dinsdale

cwd stands for “current working directory.” The result in this example is /home/dinsdale,
which is the home directory of a user named dinsdale.

A string like cwd that identifies a file is called a path. A relative path starts from the current
directory; an absolute path starts from the topmost directory in the file system.

The paths we have seen so far are simple filenames, so they are relative to the current directory.
To find the absolute path to a file, you can use os.path.abspath:

>>> os.path.abspath('memo.txt')
'/home/dinsdale/memo.txt'

os.path.exists checks whether a file or directory exists:

>>> os.path.exists('memo.txt')
True

If it exists, os.path.isdir checks whether it‟s a directory:


>>> os.path.isdir('memo.txt')
False
>>> os.path.isdir('music')
True

Similarly, os.path.isfile checks whether it‟s a file.


os.listdir returns a list of the files (and other directories) in the given directory:

>>> os.listdir(cwd)
['music', 'photos', 'memo.txt']

To demonstrate these functions, the following example “walks” through a directory, prints the
names of all the files, and calls itself recursively on all the directories.

def walk(dirname):
for name in os.listdir(dirname):
path = os.path.join(dirname, name)
if os.path.isfile(path):
print path
else:
walk(path)
os.path.join takes a directory and a file name and joins them into a complete path.

Command Line Arguments


• Python supports the creation of programs that can be run on the command line,
completely with command-line arguments.
• It provides a getopt module, that help to parse command line options and arguments.

Accessing Command Line Arguments


• The Python sys module provides access to any of the command-line arguments via
sys.argv. It solves two purposes:
• sys.argv is the list of command line arguments
• len(sys.argv) is the number of command line arguments that you have in your command
line
• sys.argv[0] is the program, i.e. script name

Executing Python
• You can execute Python in this way:
• $python text.py inp1, inp2, inp3

Example

text.py:
import sys
print ‘Number of arguments:’, len (sys.argv), ‘arguments.’
print ‘Argument List:’, str(sys.argv)

• It will produce the following output:


Number of arguments: 4 arguments.
Argument List: [‘sample.py’, ‘inp1’, ‘inp2’, ‘inp3’]
Errors and Exceptions:
Errors – referred as bugs in the program.
Errors occurs maximum by the fault of the programmer.
Debugging – Process of finding and correcting errors.

Two types of errors.:


Syntax errors
– python interpreter finds the syntax error when it executes the coding. Once find the error, it
displays the error by stopping the execution.
Common occurring syntax errors are
• Putting a keyword at wrong place
• Misspelling the keyword
• Incorrect indentation
• Forgetting symbols like comma, brackets, quotes (“or “)
• Empty block

Run time errors


• – if a program is free of syntax errors, then it runs by the interpreter and the errors occurs
during the run time of the program due to logical mistake is called runtime errors.
Examples:
• Trying to access a file that doesn’t exists
• Performing the operations like division by zero
• Using an identifier which is not defined

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. They occur, for example, when
a file we try to open does not exist (FileNotFoundError), dividing a number by zero
(ZeroDivisionError), module we try to import is not found (ImportError) etc. Whenever this
type of runtime error 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'
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
cause then are tabulated below.

Python Built-in Exceptions


AssertionError Raised when assert statement fails.
AttributeError Raised when attribute assignment or reference fails.
EOFError Raised when the input() functions hits end-of-file condition.
FloatingPointError Raised when a floating point operation fails
GeneratorExit Raise when a generator's close() method is called.
ImportError Raised when the imported module is not found.
IndexError Raised when 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 interrupt key (Ctrl+c or delete).
MemoryError Raised when an operation runs out of memory.
NameError Raised when a variable is not found in local or global scope.
NotImplementedError Raised by abstract methods.
OSError Raised when system operation causes system related error.
OverflowError Raised when result of an arithmetic operation is 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 next() function to indicate that there is no further item to
be returned by iterator.
SyntaxError Raised by parser when syntax error is encountered.
IndentationError Raised when there is incorrect indentation.
SystemError Raised when interpreter detects internal error.
SystemExit Raised by sys.exit() function.
TypeError Raised when a function or operation is applied to an object of
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.
ValueError Raised when a function gets argument of correct type but improper
value.
ZeroDivisionError Raised when 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.
Python Exception Handling
Python has many built-in exceptions which forces 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 come to a sudden, unexpected
halt.

Catching Exceptions in Python

In Python, exceptions can be handled using a try statement.

A critical operation which can raise exception is placed inside the try clause and the code that
handles exception is written in except clause.

It is up to us, what operations we perform once we have caught the exception. Here is a
simple example.

# import module sys to get the type of exception


import sys
randomList = ['a', 0, 2]
for entry in randomList:
try:
print("The entry is", entry)
r = 1/int(entry)
break
except:
print("Oops!",sys.exc_info()[0],"occured.")
print("Next entry.")
print()
print("The reciprocal of",entry,"is",r)

Output
The entry is a
Oops! <class 'ValueError'> occured.
Next entry.
The entry is 0
Oops! <class 'ZeroDivisionError' > occured.
Next entry.
The entry is 2
The reciprocal of 2 is 0.5

In this program, we loop until the user enters an integer that has a valid reciprocal. The portion
that can cause exception is placed inside try block.
If no exception occurs, except block is skipped and normal flow continues. But if any exception
occurs, it is caught by the except block.

Here, we print the name of the exception using ex_info() function inside sys module and ask
the user to try again. We can see that the values 'a' and '1.3' causes ValueError and '0' causes
ZeroDivisionError.

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 network) are performed in the
finally clause to guarantee execution. Here is an example of file operations to illustrate this.

try:
f = open("test.txt",encoding = 'utf-8')
# perform file operations
finally:
f.close()

Exceptions versus Syntax Errors

Syntax errors occur when the parser detects an incorrect statement. Observe the following
example:

>>> print( 0 / 0 ))
File "<stdin>", line 1
print( 0 / 0 ))
^
SyntaxError: invalid syntax

The arrow indicates where the parser ran into the syntax error. In this example, there was one
bracket too many. Remove it and run your code again:

>>> print( 0 / 0)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ZeroDivisionError: integer division or modulo by zero

This time, you ran into an exception error. This type of error occurs whenever syntactically
correct Python code results in an error. The last line of the message indicated what type of
exception error you ran into.
Instead of showing the message exception error, Python details what type of exception error
was encountered. In this case, it was a ZeroDivisionError. Python comes with various built-in
exceptions as well as the possibility to create self-defined exceptions.

What is Exception?

An exception is an event, which occurs during the execution of a program that disrupts the
normal flow of the program's instructions. In general, when a Python script encounters a
situation that it cannot cope with, it raises an exception. An exception is a Python object that
represents an error.

When a Python script raises an exception, it must either handle the exception immediately
otherwise it terminates and quits.

List of Standard Exceptions


Sr.No. Exception Name
1 Exception
Base class for all exceptions
2 StopIteration
Raised when the next() method of an iterator does not point to any object.
3 SystemExit
Raised by the sys.exit() function.
4 StandardError
Base class for all built-in exceptions except StopIteration and SystemExit.
5 ArithmeticError
Base class for all errors that occur for numeric calculation.
6 OverflowError
Raised when a calculation exceeds maximum limit for a numeric type.
7 FloatingPointError
Raised when a floating-point calculation fails.
8 ZeroDivisionError
Raised when division or modulo by zero takes place for all numeric types.
9 AssertionError
Raised in case of failure of the Assert statement.
10 AttributeError
Raised in case of failure of attribute reference or assignment.
11 EOFError
Raised when there is no input from either the raw_input() or input() function and
the end of file is reached.
12 ImportError
Raised when an import statement fails.
13 KeyboardInterrupt
Raised when the user interrupts program execution, usually by pressing Ctrl+c.
14 LookupError
Base class for all lookup errors.
15 IndexError
Raised when an index is not found in a sequence.
16 KeyError
Raised when the specified key is not found in the dictionary.
17 NameError
Raised when an identifier is not found in the local or global namespace.
18 UnboundLocalError
Raised when trying to access a local variable in a function or method but no value
has been assigned to it.
19 EnvironmentError
Base class for all exceptions that occur outside the Python environment.
20 IOError
Raised when an input/ output operation fails, such as the print statement or the
open() function when trying to open a file that does not exist.
21 IOError
Raised for operating system-related errors.
22 SyntaxError
Raised when there is an error in Python syntax.
23 IndentationError
Raised when indentation is not specified properly.
24 SystemError
Raised when the interpreter finds an internal problem, but when this error is
encountered the Python interpreter does not exit.
25 SystemExit
Raised when Python interpreter is quit by using the sys.exit() function. If not
handled in the code, causes the interpreter to exit.
26 TypeError
Raised when an operation or function is attempted that is invalid for the specified
data type.
27 ValueError
Raised when the built-in function for a data type has the valid type of arguments,
but the arguments have invalid values specified.
28 RuntimeError
Raised when a generated error does not fall into any category.
29 NotImplementedError
Raised when an abstract method that needs to be implemented in an inherited class
is not actually implemented.

Handling an exception

If you have some suspicious code that may raise an exception, you can defend your program
by placing the suspicious code in a try: block. After the try: block, include an except: statement,
followed by a block of code which handles the problem as elegantly as possible.

The except Clause with No Exceptions

You can also use the except statement with no exceptions defined as follows −
try:
You do your operations here;
......................
except:
If there is any exception, then execute this block.
......................
else:
If there is no exception then execute this block.

This kind of a try-except statement catches all the exceptions that occur. Using this kind of try-
except statement is not considered a good programming practice though, because it catches all
exceptions but does not make the programmer identify the root cause of the problem that may
occur.

Syntax
Here is simple syntax of try....except...else blocks −

try:
You do your operations here;
......................
except ExceptionI:
If there is ExceptionI, then execute this block.
except ExceptionII:
If there is ExceptionII, then execute this block.
......................
else:
If there is no exception then execute this block.

• A single try statement can have multiple except statements. This is useful when the try
block contains statements that may throw different types of exceptions.
• You can also provide a generic except clause, which handles any exception.
• After the except clause(s), you can include an else-clause. The code in the else-block
executes if the code in the try: block does not raise an exception.
• The else-block is a good place for code that does not need the try: block's protection.

Example This example opens a file, writes content in the file

try:
fh = open("testfile", "w")
fh.write("This is my test file for exception handling!!")
except IOError:
print "Error: can\'t find file or read data"
else:
print "Written content in the file successfully"
fh.close()

This produces the following result −

Written content in the file successfully

Example

This example tries to open a file where you do not have write permission, so it raises an
exception −

try:
fh = open("testfile", "r")
fh.write("This is my test file for exception handling!!")
except IOError:
print "Error: can\'t find file or read data"
else:
print "Written content in the file successfully"

This produces the following result –

Error: can't find file or read data


Error: can't find file or read data

The except Clause with Multiple Exceptions

You can also use the same except statement to handle multiple exceptions as follows −

try:
You do your operations here;
......................
except(Exception1[, Exception2[,...ExceptionN]]]):
If there is any exception from the given exception list, then execute this block.
......................
else:
If there is no exception then execute this block.

The try-finally Clause

You can use a finally: block along with a try: block. The finally block is a place to put any code
that must execute, whether the try-block raised an exception or not. The syntax of the try-finally
statement is this −
try:
You do your operations here;
......................
Due to any exception, this may be skipped.
finally:
This would always be executed.
......................

You cannot use else clause as well along with a finally clause.

Example
#!/usr/bin/python
try:
fh = open("testfile", "w")
fh.write("This is my test file for exception handling!!")
finally:
print "Error: can\'t find file or read data"

If you do not have permission to open the file in writing mode, then this will produce the
following result −
Error: can't find file or read data
Same example can be written more cleanly as follows −
#!/usr/bin/python
try:
fh = open("testfile", "w")
try:
fh.write("This is my test file for exception handling!!")
finally:
print "Going to close the file"
fh.close()
except IOError:
print "Error: can\'t find file or read data"

When an exception is thrown in the try block, the execution immediately passes to the finally
block. After all the statements in the finally block is executed, the exception is raised again and
is handled in the except statements if present in the next higher layer of the try-except
statement.

Argument of an Exception

An exception can have an argument, which is a value that gives additional information about
the problem. The contents of the argument vary by exception. You capture an exception's
argument by supplying a variable in the except clause as follows −

try:
You do your operations here;
......................
except ExceptionType, Argument:
You can print value of Argument here...

If you write the code to handle a single exception, you can have a variable follow the name of
the exception in the except statement. If you are trapping multiple exceptions, you can have a
variable follow the tuple of the exception.

This variable receives the value of the exception mostly containing the cause of the exception.
The variable can receive a single value or multiple values in the form of a tuple. This tuple
usually contains the error string, the error number, and an error location.

Example
Following is an example for a single exception −

#!/usr/bin/python
# Define a function here.
def temp_convert(var):
try:
return int(var)
except ValueError, Argument:
print "The argument does not contain numbers\n", Argument
# Call above function here.
temp_convert("xyz");
This produces the following result −
The argument does not contain numbers invalid literal for int() with base 10: 'xyz'

Raising an Exceptions

You can raise exceptions in several ways by using the raise statement. The general syntax for
the raise statement is as follows.

Syntax
raise [Exception [, args [, traceback]]]

Here, Exception is the type of exception (for example, NameError) and argument is a value for
the exception argument. The argument is optional; if not supplied, the exception argument is
None.

The final argument, traceback, is also optional (and rarely used in practice), and if present, is
the traceback object used for the exception.

Example

An exception can be a string, a class or an object. Most of the exceptions that the Python core
raises are classes, with an argument that is an instance of the class. Defining new exceptions is
quite easy and can be done as follows −

def functionName( level ):


if level < 1:
raise "Invalid level!", level
# The code below to this would not be executed
# if we raise the exception

Note: In order to catch an exception, an "except" clause must refer to the same exception thrown
either class object or simple string. For example, to capture above exception, we must write the
except clause as follows −

try:
Business Logic here...
except "Invalid level!":
Exception handling here...
else:
Rest of the code here...

User-Defined Exceptions

Python also allows you to create your own exceptions by deriving classes from the standard
built-in exceptions.

Here is an example related to RuntimeError. Here, a class is created that is subclassed from
RuntimeError. This is useful when you need to display more specific information when an
exception is caught.
In the try block, the user-defined exception is raised and caught in the except block. The
variable e is used to create an instance of the class Networkerror.

class Networkerror(RuntimeError):
def __init__(self, arg):
self.args = arg

So once you defined above class, you can raise the exception as follows −
try:
raise Networkerror("Bad hostname")
except Networkerror,e:
print e.args

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

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.
Eg:
# 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 in order for Python to consider it as a
package. This file can be left empty but we generally place the initialization code for that
package in this file. 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.

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
as for module search path.

Accessing Packages:
• Step 1: Create a folder name “MyPackage” in the folder where the python files are
storing.
• Step 2: Create a subfolder name “Add” in the folder “MyPackage”.
• Step 3: Type a python program containing the function to add two numbers with
function name “add” and save the file inside the folder “Add” by the name “addition”

import MyPackage.Add.addition
a = int(input("Enter first number:"))
b = int(input("Enter second number:"))
print(MyPackage.Add.addition.add(a,b))

output
Enter first number: 43
Enter second number: 23
66

ILLUSTRATIVE PROGRAMS

Word Count

import sys
file=open("D:/PO.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]))

Copy file

Method 1
with open("test.txt") as f:
with open("out.txt", "w") as f1:
for line in f:
f1.write(line)

Method 2
# Python Program - Copy Files
from shutil import copyfile;
print("Enter 'x' for exit.");
sourcefile = input("Enter source file name (copy from): ");
if sourcefile == 'x':
exit();
else:
destinationfile = input("Enter destination file name (copy to): ");
copyfile(sourcefile, destinationfile);
print("File copied successfully!");
print("Want to display the content ? (y/n): ");
check = input();
if check == 'n':
exit();
else:
c = open(destinationfile, "r");
print(c.read());
c.close();

Voter’s age validation

while True:
try:
age = int(input("Please enter your age: "))
except ValueError:
print("Sorry, I didn't understand that.")
continue

if age < 0:
print("Sorry, your response must not be negative.")
continue
else:
#age was successfully parsed, and we're happy with its value.
#we're ready to exit the loop.
break
if age >= 18:
print("You are able to vote!")
else:
print("You are not able to vote.")

Output:
Please enter your age: a
Sorry, I didn't understand that.
Please enter your age: -1
Sorry, your response must not be negative.
Please enter your age: 16
You are not able to vote.

Marks range validation (0-100)

while True:
try:
coursework = int(input("Enter the Coursework Mark: "))
except ValueError:
print("Sorry, I didn't understand that.")
continue

if coursework < 1 or coursework > 100:


print("The Coursework Mark value is out of range, try again.")
else:
print("The Coursework Mark entered is Valid.")
break

Output:
Enter the Coursework Mark: a
Sorry, I didn't understand that.
Enter the Coursework Mark: 0
The Coursework Mark value is out of range, try again.
Enter the Coursework Mark: -1
The Coursework Mark value is out of range, try again.
Enter the Coursework Mark: 89
The Coursework Mark entered is Valid.

You might also like