Ge1105 Problem Solving and Python Programming Unit - 5: 1. Opening A File
Ge1105 Problem Solving and Python Programming Unit - 5: 1. Opening A File
Ge1105 Problem Solving and Python Programming Unit - 5: 1. Opening A File
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
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
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
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
P a g e 5 | 18
St. Joseph’s College of Engineering
GE1105 PROBLEM SOLVING AND PYTHON PROGRAMMING UNIT - 5
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'
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
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
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
yy= 2017
mm = 8
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
❖ 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
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
1. Write a function that copies a file reading and writing up to 50 characters at a time.
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
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()
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
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.
P a g e 18 | 18
St. Joseph’s College of Engineering