GE4105 - PSPP - Unit V - Notes
GE4105 - PSPP - Unit V - Notes
GE4105 - PSPP - Unit V - Notes
Other programs are persistent: they run for a long time (or all the time); they keep at least
some of their data in permanent storage (a hard drive, for example); and if they shut down
and restart, they pick up where they left off.
One of the simplest ways for programs to maintain their data is by reading and
writing text files. An alternative is to store the state of the program in a database.
Python provides inbuilt functions for creating, writing and reading files. There
are two types of files that can be handled in python, normal text files and binary files (written
in binary language,0s and 1s).
➢ 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.
➢ 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.
In order to perform some operations on files we have to follow below steps
➢ Opening
➢ Reading or writing
➢ Closing
Here we are going to discusses about opening, closing, reading and writing data in a text file.
➢ Read Only (‘r’) : Open text file for reading. The handle is positioned at the beginning
of the file. If the file does not exists, raises I/O error. This is also the default mode in
which file is opened.
➢ Read and Write (‘r+’) : Open the file for reading and writing. The handle is positioned
at the beginning of the file. Raises I/O error if the file does not exists.
➢ Write Only (‘w’) : Open the file for writing. For existing file, the data is truncated and
over-written. The handle is positioned at the beginning of the file. Creates the file if the
file does not exists.
➢ Write and Read (‘w+’) : Open the file for reading and writing. For existing file, data
is truncated and over-written. The handle is positioned at the beginning of the file.
➢ Append Only (‘a’) : Open the file for writing. The file is created if it does not exist.
The handle is positioned at the end of the file. The data being written will be inserted
at the end, after the existing data.
➢ Append and Read (‘a+’) : Open the file for reading and writing. The file is created if
it does not exist. The handle is positioned at the end of the file. The data being written
will be inserted at the end, after the existing data.
>>> f1 = open("sample.txt","a")
>>> f2 = open(r"G:\class\python\sample3.txt","w+")
seek():The seek(offset[, from]) method changes the current file position. The
offset argument indicates the number of bytes to be moved. The from argument specifiesthe
reference position from where the bytes are to be moved.
If from is set to 0, it means use the beginning of the file as the reference position
and 1 means use the current position as the reference position and if it is set to 2 then the end
of the file would be taken as the reference position. If the second argument is omitted, it also
means use the beginning of the file as the reference position.
Example:
>>> f1=open("G:\class\python\code\sample.txt","r")
>>>f1.tell()
0L
>>>f1.readline()
'Read Only\n'
>>>f1.tell()
11L
>>>f1.seek(0)
>>>f1.tell()
0L
>>>f1.seek(5)
>>>f1.tell()
5L
>>>f1.readline()
'Only\n'
>>> f4=open("fruit.txt","w")
>>>fruit_list=['Apple\n','Orange\n','Pineapple\n']
>>>f4.writelines(fruit_list)
>>>f4.write('Strawberry\n')
>>>f4.close()
>>> f4=open("fruit.txt","r")
>>>f4.read()
'Apple\nOrange\nPineapple\nStrawberry\n'
5.1.8 Appending to a File
Adding content at the end of a file is known as append. In order to do
appending operation, we have to open the file with append mode.
Example:
>>> f4=open('fruit.txt','a')
>>>f4.write('Banana')
>>>f4.close()
>>> f4=open('fruit.txt','r')
>>>f4.read()
'Apple\nOrange\nPineapple\nStrawberry\nBanana\n'
File_object.softspace Returns false if space explicitly required with print, true otherwise.
Example 1
Consider the following script command_line.py
import sys
print 'There are %d arguments'%len(sys.argv)
print 'Argument are', str(sys.argv)
print 'File Name is: ', sys.argv[0]
Now run above script as follows – in Command prompt:
C:\Python27>python.exe command_line.py vinuranjith
This produce following result –
There are 3 arguments
Argument are ['command_line.py', 'vinu', 'ranjith']
File Name is: command_line.py
NOTE: As mentioned above, first argument is always script name and it is also being counted
in number of arguments. Here ‗vinu‘ and ‗ranjith‘ are extra inputs passed toprogram
through command line argument method while running python program command_line.py.
Example 2
This is a Python Program to copy the contents of one file into another. Source
and destination file names are given through command line argument while running the
program.
1) Open file name with command line argument one as read mode (input file).
2) Open file name with command line argument two as write mode (output file).
3) Read each line from the input file and write it into the output file until the input file
data getsover.
4) Exit.
Program
import sys
source=open(sys.argv[1],'r')
destination=open(sys.argv[2],'w')
while(True):
new_line=source.readline()
ifnew_line=='':
break
destination.write(new_line)
source.close()
destination.close()
Now run above script as follows – in Command prompt:
C:\Python27>python.exe copy_file.py input_file.txt output_file.txt
5.3.2 Exceptions
Even if a statement or expression is syntactically correct, it may cause an error when an attempt is
made to execute it. Errors detected during execution are called exceptions. You will soon learn how
to handle them in Python programs. Most exceptions are not handled by programs, however, and
result in error messages as shown here:
>>> 55+(5/0)
Traceback (most recent call last):
File "<pyshell#12>", line 1, in <module>
55+(5/0)
ZeroDivisionError: integer division or modulo by zero
>>> 5+ repeat*2
Traceback (most recent call last):
File "<pyshell#13>", line 1, in <module>
5+ repeat*2
NameError: name 'repeat' is not defined
>>> '5'+5
Traceback (most recent call last):
File "<pyshell#14>", line 1, in <module>
'5'+5
TypeError: cannot concatenate 'str' and 'int' objects
The last line of the error message indicates what happened. Exceptions come
in different types, and the type is printed as part of the message: the types in the example
are ZeroDivisionError, NameError and TypeError. The string printed as the exception type
is the name of the built-in exception that occurred. This is true for all built-in exceptions, but
need not be true for user-defined exceptions (although it is a useful convention). Standard
exception names are built-in identifiers (not reserved keywords).
The rest of the line provides detail based on the type of exception and what caused it.
The preceding part of the error message shows the context where the exception
happened, in the form of a stack traceback. In general it contains a stack traceback listing source
lines; however, it will not display lines read from standard input.
5.4HANDLING EXCEPTIONS
Python provides two very important features to handle any unexpected error in
your Python programs and to add debugging capabilities in them.
➢ Exception Handling:
➢ Assertions:
When a Python script raises an exception, it must either handle the exception
immediately otherwise it terminates and quits.
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.
Syntax:
try:
You do your operations here;
......................
exceptExceptionI:
If there is ExceptionI, then execute this block.
exceptExceptionII:
If there is ExceptionII, then execute this block.
......................
else:
If there is no exception then execute this block.
Here are few important points about the above-mentioned syntax −
➢ 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 with write mode, writes content in the file and comes
out gracefully because there is no problem at all
try:
fp = open("test_exception.txt", "w")
fp.write("Exception handling")
exceptIOError:
print "Error: File don\'t have read permission"
else:
print "Written successfully"
fp.close()
This produces the following result:
Written successfully
Example:
This example opens a file with read mode, and tries to write the file where you
do not have write permission, so it raises an exception
try:
fp = open("test_exception.txt", "r")
fp.write("Exception handling")
exceptIOError:
print "Error: File don\'t have read permission"
else:
print "Written successfully"
fp.close()
This produces the following result
Error: File don't have read permission
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.
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.
5.4.4 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
This example opens a file with write mode, writes content in the file and comes
out gracefully because there is no problem at all
try:
fp = open("test_exception.txt", "w")
fp.write("Exception handling")
exceptIOError:
print "Error: File don\'t have read permission"
else:
print "Written successfully"
finally:
print "Closing file"
fp.close()
This produces the following result
Written successfully
Closing file
Example
This example opens a file with read mode, and tries to write the file where you
do not have write permission, so it raises an exception
try:
fp = open("test_exception.txt", "r")
fp.write("Exception handling")
exceptIOError:
print "Error: File don\'t have read permission"
else:
print "Written successfully"
finally:
print "Closing file"
fp.close()
This produces the following result
Error: File don't have read permission
Closing file
In the above two examples, one script didn‘t raise exception and another script
raise exception. But we can see that in both cases finally block gets executed.
try:
You do your operations here;
......................
exceptExceptionType, 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
deftemp_convert(var):
try:
returnint(var)
exceptValueError, Argument:
print "The argument is not a numbers\n", Argument
temp_convert("abc")
This produces the following result
The argument is not a numbers
invalid literal for int() with base 10: 'abc'
defthis_fails():
x = 1/0
try:
this_fails()
exceptZeroDivisionError, detail:
print 'Handling run-time error:', detail
This produces the following result
Handling run-time error: integer division or modulo by zero
In this example exception is raised in this_fails() function. But, because of
this_fails() function don‘t have except block exception is thrown to the caller function. As there
is a except block, it will handle the exception.
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 −
deffunctionName( level ):
if level < 1:
raise "Invalid level!", level
# if we raise the exception,code below to this not executed
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...
An assertion is a sanity-check that you can turn on or turn off when you are
done with your testing of the program.
Assertions are carried out by the assert statement, the newest keyword to
Python, introduced in version 1.5.
Programmers often place assertions at the start of a function to check for valid
input, and after a function call to check for valid output.
Example
defKelvinToFahrenheit(Temperature):
assert (Temperature >= 0),"Colder than absolute zero!"
return ((Temperature-273)*1.8)+32
printKelvinToFahrenheit(275)
printint(KelvinToFahrenheit(509.25))
printKelvinToFahrenheit(-7)
When the above code is executed, it produces the following result
35.6
457
5.5 MODULES
A module allows you to logically organize your Python code. Grouping related
code into a module makes the code easier to understand and use. A module is a file that
contains a collection of related functions. Python has lot of built-in modules; math module is
one of them. math module provides most of the familiar mathematical functions.
Before we can use the functions in a module, we have to import it with an import statement:
>>>math
<module 'math' (built-in)>
The module object contains the functions and variables defined in the module.
To access one of the functions, you have to specify the name of the module and the name of
the function, separated by a dot (also known as a period). This format is called dot notation.
>>>math.log10(200)
2.3010299956639813
>>>math.sqrt(10)
3.1622776601683795
Math module have functions like log(), sqrt(), etc… In order to know what are
the functions available in particular module, we can use dir() function after importing particular
module. Similarly if we want to know detail description about a particular module or function
or variable means we can use help() function.
Example
>>> import math
>>>dir(math)
[' doc ', ' name ', ' package ', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh',
'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor',
'fmod', 'frexp', 'fsum', 'gamma', 'hypot', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p',
'modf', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'trunc']
>>>help(pow)
Help on built-in function pow in module builtin :
pow(...)
pow(x, y[, z]) -> number
5.5.1Writing Modules
Any file that contains Python code can be imported as a module. For example,
suppose you have a file named addModule.py with the following code:
def add(a, b):
result = a + b
print(result)
add(10,20)
If you run this program, it will add 10 and 20 and print 30. We can import it like this:
>>> import addModule
30
Now you have a module object addModule
>>>addModule
<module 'addModule' from 'G:/class/python/code\addModule.py'>
The module object provides add():
>>>addModule.add(120,150)
270
So that‘s how you write modules in Python.
The only problem with this example is that when you import the module it
runs the test code at the bottom. Normally when you import a module, it defines new functions
but it doesn‘t run them.
Programs that will be imported as modules often use the following idiom:
if name == ' main ':
add(10,20)
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 runs.
Otherwise, if the module is being imported, the test code is skipped. Modify addModule.py file
as given below.
def add(a, b):
result = a + b
print(result)
if name == ' main ':
add(10,20)
Now while importing addModule test case is not running
>>> import addModule
name has module name as its value when it is imported. Warning: If you
import a module that has already been imported, Python does nothing. It does not re-read the
file, even if it has changed. If you want to reload a module, you can use the built-in function
reload, but it can be tricky, so the safest thing to do is restart the interpreter and then import the
module again.
5.6 PACKAGES
The init .py file can also decide which modules the package exports as the API, while keeping
other modules internal, by overriding the all variable, like so:
init .py:
all = ["sample_module"]
Example.
Following program print each word in the specified file occurs how many times.
import sys
defword_count(file_name):
try:
file=open(file_name,"r")
wordcount={}
entier_words=file.read().split()
for word in entier_words:
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]))
exceptIOError:
print ("No file found with name %s" %file_name)
fname=raw_input("Enter New File Name:")
word_count(fname)
try:
word_count(sys.argv[1])
exceptIndexError:
print("No file name passed as command line Argument")
fname=raw_input("Enter File Name:")
word_count(fname)
Content of a sample file word_count_input.txt is:
word count is the program which count each word in file appears how many times.
try:
word_count(sys.argv[1])
exceptIndexError:
print("No file name passed as command line Argument")
fname=raw_input("Enter File Name:")
word_count(fname)
This produces the following result
c:\Python27>python wordcount2.py
No file name passed as command line Argument
Enter File Name:word_count_input
No file found with name word_count_input
Enter New File Name:word_count_input.txt
word_count_input.txt File have 15 words
Above programalso shows the file handling with exception handling and
command line argument.While running, if you give command line like below, it will read
the text file with the name, that is specifies by command line argument one and count
number of word present in it and print it.
c:\Python27>python wordcount2.py word_count_input.txt
While running, if you give command line like below ( ie, no command line
argument), On behalf of exception handling it will ask for file name and then do the same
word counting operation on the newly entered file.
c:\Python27>python wordcount2.py
Program also handle file not found exception, if wrong file name is entered. It
will ask for new file name to enter and proceed.
This is a Python Program to copy the contents of one file into another. In order to perform
the copying operation we need to follow the following steps.
1. Open source file in read mode.
2. Open destination file in write mode.
3. Read each line from the input file and write it into the output file.
4. Exit.
Also we include command line argument for passing source and destination file names to
program. Also exception handling is used to handle exception that occurs when dealing with
files.
import sys
def copy(src,dest):
try:
source=open(src,'r')
destination=open(dest,'w')
while(True):
new_line=source.readline()
ifnew_line=='':
break
destination.write(new_line)
source.close()
destination.close()
exceptIOError:
print ("Problem with Source or Destination File Name ")
source_name=raw_input("Enter New Source File Name:")
destination_name=raw_input("Enter New Destination File Name:")
copy(source_name,destination_name)
try:
copy(sys.argv[1],sys.argv[2])
exceptIndexError:
print("Insufficent Command line argument!")
source_name=raw_input("Enter Source File Name:")
destination_name=raw_input("Enter Destination File Name:")
copy(source_name,destination_name)
finally:
print("Copying Done.....")
This produces the following result
C:\Python27>python copy_file_exception.py input_file.txt
Insufficent Command line argument!
Enter Source File Name:input_file.tx
Enter Destination File Name:output_file.txt
Problem with Source or Destination File Name
Enter New Source File Name:input_file.txt
Enter New Destination File Name:output_file.txt
Copying Done.....