23CS101T PSPP - Unit 5
23CS101T PSPP - Unit 5
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)
FILE
A file is a collection of data stored on a secondary storage device like hard disk. A file
is basically used because real-life applications involve large amounts of data and in
such situations the console oriented I/O operations pose two major problems:
First, it becomes cumbersome and time consuming to handle huge amount of data
through terminals.
Second, when doing I/O using terminal, the entire data is lost when either the program
is terminated or computer is turned off. Therefore, it becomes necessary to store data on
a permanent storage (the disks) and read whenever necessary, without destroying the
data.
File Path
Files are stored on a storage medium like the hard disk in such a way that they can be
easily retrieved as and when required.
Every file is identified by its path that begins from the root node or the root folder.
In Windows, C:\ (also known as C drive) is the root folder but you can also have a path
that starts from other drives like D:\, E:\, etc. The file path is also known as pathname.
A file path can be either relative or absolute.
Absolute Path : Absolute path always contains the root and the complete directory list
to specify the exact location the file.
Ex. C:\Students\Under Graduate\BTech_CS.docx
Relative Path : Relative path needs to be combined with another path in order to
access a file. It starts with respect to the current working directory and therefore lacks
the leading slashes.
Ex. Under Graduate\BTech_CS.docx is a relative path as only a part of thecomplete
path is specified.
Types of Files
Text File :
A text file is a stream of characters that can be sequentially processed by a computer in
forward direction. For this reason a text file is usually opened for only one kind of
operation (reading, writing, or appending) at any given time. Because text files can
process characters, they can only read or write data one character at a time.
Depending on the requirements of the operating system and on the operation that has to
be performed (read/write operation) on the file, the newline characters may be
converted to or from carriage-return/linefeed combinations. Besides this, other
character conversions may also be done to satisfy the storage requirements of the
operating system. However, these conversions occur transparently to process a text file.
In a text file, each line contains zero or more characters and ends with one or more
characters
Another important thing is that when a text file is used, there are actually two
representations of data- internal or external. For example, an integer value will be
represented as a number that occupies 2 or 4 bytes of memory internally but externally
the integer value will be represented as a string of characters representing its decimal or
hexadecimal value.
Binary File:
A binary file is a file which may contain any type of data, encoded in binary form for
computer storage and processing purposes. It includes files such as word processing
documents, PDFs, images, spreadsheets, videos, zip files and other executable
programs. Like a text file, a binary file is a collection of bytes. A binary file is also
referred to as a character stream with following two essential differences.
• A binary file does not require any special processing of the data and each byte of data
is transferred to or from the disk unprocessed.
• Python places no constructs on the file, and it may be read from, or written to, in any
manner the programmer wants.
While text files can be processed sequentially, binary files, on the other hand, can be
either processed sequentially or randomly depending on the needs of the application.
Opening files
Before reading from or writing to a file, it must first be opened using Python‘sbuilt-in
open() function.
open ( ) function creates a file object, which will be used to invoke methods
assosciated with it.
The syntax of open() is:
fileObj = open(file_name [, access_mode])
Here,
file_name is a string value that specifies name of the file that you want to access.
access_modeindicates the mode in which the file has to be opened, i.e., read, write,
append, etc.
open function() -Access Modes
Mode Purpose
r Opens a file for reading only. The file pointer is placed at the beginning of
the file. This is the default mode.
rb Opens a file for reading only in binary format. The file pointer is placed at
the beginning of the file. This is the default mode.
r+ Opens a file for both reading and writing. The file pointer placed at the
beginning of the file.
rb+ Opens a file for both reading and writing in binary format. The file pointer
placed at the beginning of the file.
w Opens a file for writing only. Overwrites the file if the file exists. If the file
does not exist, creates a new file for writing.
wb Opens a file for writing only in binary format. Overwrites the file if the file
exists. If the file does not exist, creates a new file for writing.
w+ Opens a file for both writing and reading. Overwrites the existing file if the
file exists. If the file does not exist, creates a new file for reading and writing.
wb+ Opens a file for both writing and reading in binary format. Overwrites the
existing file if the file exists. If the file does not exist, creates a new file for
reading and writing.
a Opens a file for appending. The file pointer is at the end of the file if the file
exists. That is, the file is in the append mode. If the file does not exist, it
creates a new file for writing.
ab Opens a file for appending in binary format. The file pointer is at the end of
the file if the file exists. That is, the file is in the append mode. If the file does
not exist, it creates a new file for writing.
a+ Opens a file for both appending and reading. The file pointer is at the end of
the file if the file exists. The file opens in the append mode. If the file does
not exist, it creates a new file for reading and writing.
ab+ Opens a file for both appending and reading in binary format. The file pointeris
at the end of the file if the file exists. The file opens in the append mode. Ifthe
file does not exist, it creates a new file for reading and writing.
Program
file = open (“File1.txt”, “r”)
print (file)
Output
< open file „File1.txt‟, mode „r‟ at 0x02A850D0 >
Closing files
close() Method
The close() method is used to close the file object.
While closing the file object the close() flushes any unwritten information. Once a file
object is closed, reading and from or writing into the file associated with the file object
cannot be further done.
Although, Python automatically closes a file when the reference object of a file is
reassigned to another file, but as a good programming habit to always explicitly use the
close() method to close a file.
The syntax of close() is
fileObj.close()
The close() method frees up any system resources such as file descriptors, file locks,
etc. that are associated with the file.
Moreover, there is an upper limit to the number of files a program can open. If that
limit is exceeded then the program may even crash or work in unexpected manner.
Lots of memory is wasted if many files are open unnecessarily and open files always
stand a chance of corruption and data loss.
Once the file is closed using the close() method, any attempt to use the file object will
result in an error.
File object Attributes
Once a file is successfully opened, a file object is returned.
Using this file object, different type of information related to that file is easily accessed
by reading specific attributes of the file.
Attribute Description
Program:
file = open (“File1.txt”, “w”)
print (“Name of the file :”, file.name)
print (“File is closed:”, file.closed)
print (“File has been opened in”, file.mode,”mode”)
Output
Name of the file : File1.txt
File is closed: False
File has been opened in w mode
Reading files
1. The read method
The read() method is used to read a string from an already opened file.
The string can include, alphabets, numbers, characters or other symbols.
using 'r' or 'rb' mode and then start reading
data.
The syntax of read() method is
fileObject.read([size])
where size is an optional parameter which if passed to the read() method specifies
the number of bytes to be read from the opened file.
The read() method starts reading from the beginning of the file and if size is missing
or has a negative value then, it reads the entire contents of the file (i.e., till the end
of file).
Program
file = open (“File1.txt”, “r”)
print (file.read(10))
file.close ( )
Output
Hello All
2. readline method
The readline( ) method is used to read individual lines of a file. It reads a file
untilthe newline (\n) character is reached or end of file (EOF).
Syntax :
fileObject.readline([size])
Here size is the optional numeric argument and returns a quantity of data equal to
size.
Program
file = open (“File1.txt”, “r”)
print (file.readline())
print (file.readline())
print (file.readline())
file.close ( )
Output
3. readlines method
The readlines() method returns a list of remaining lines on the entire file. It returns
empty string when end of file (EOF) is reached.
Syntax :
fileObject.readlines([size])
Here size is the optional numeric argument and returns a quantity of data equal to
size.
Program
file = open (“File1.txt”, “r”)
print (file.readlines( ))
file.close ( )
Output
[„Chennai\n‟,„Tamilnadu\n‟ , „India\n‟]
Output
Line1: Geeks
Line2: for
Line3: Geeks
Writing files
1. The write() Method
The write() method is used to write a string to an already opened file.
include numbers, special characters or other symbols.
('\n') to the
end of the string.
'w' or 'wb' mode and then start writing data
into it, where its existing contents would be overwritten.
The syntax of write() method is:
fileObject.write(string)
Program
file = open (“File1.txt”, “w”)
file.write(“ Hello All, hope everyone are enjoying with
Python”)
file.close ( )
print (“Data written into the file“)
Output
Data written into the file
2. writelines ( ) Method
The writelines() method is used to write a sequence of strings into the file.
The sequence can be any iterable object which produces a list of strings. It has no
return value.
The syntax of writelines ( ) method is:
fileObject.writelines (sequence)
Program
file = open (“File1.txt”, “w”)
lines=[“Hello world”, “Welcome”, “Enjoy learning
Python”]
file.writelines(lines)
file.close ( )
print (“List data written into the file“)
Output
List data written into the file
3. To append data and write in files
is to open the stored file again to write more data or append data to it
instead of overwriting.
using 'a' or 'ab' mode depending on whether it is a
text file or a binary file.
or combining a
large set of data into one file.
Program
file = open (“File1.txt”, “a”)
file.write(“ Python is an Interpreter based language”)
file.close ( )
print (“Data appended into the file“)
Output
Data appended into the file
2. fileno ( )
This method returns the integer file descriptor(file no.) which is used to request I/O
operations from the OS.
Syntax :fileObject.fileno ( )
Program
file = open (“File1.txt”, “w”)
print (“Name of file :”, f.name)
filedes=file.fileno( )
print(“File Descriptor :”,filedes)
file.close ( )
Output
Name of file : File1.txt
File Descriptor : 3
3. isatty ()
The isatty( ) method returns True, if the file is connected or associated with a
terminal device to atty device, else it returns False.
Syntax :fileObject.isatty ( )
Program
file = open (“File1.txt”, “w”)
print (“Name of file :”, f.name)
return = file.isatty( )
print(“Return value:”, return)
file.close ( )
Output
Name of file : File1.txt
Return value: False
4. seek ( )
seek ( ) method is used to set the position of the file pointer or in simpler
terms, move the file pointer to a new location in a file.
Syntax : fileObject.seek(offset[, from])
The offset argument indicates the number of bytes to be moved and the from
argument specifies the reference position from where the bytes are to be
moved.
3 possible values of from reference position are
0: sets the reference point at the beginning of the file 1: sets the reference point at the
current file position 2: sets the reference point at the end of the file
Program
fo = open("foo.txt", "r+")
print ("Name of the file: ", fo.name)
Output
Name of the file: foo.txt Read Line: This is 1st line.Read Line: This is 1st line.
5. tell ( ) method
The tell ( ) method returns the current position of the file read / writepointer within
the file.
It confirms that the current file position has been moved.
Syntax :
fileObject.tell (offset[, from] )
Program
# Open a file
fo= open("foo.txt","rw+") print"Name of the file: ", fo.name
Output
Name of the file: foo.txt Read Line: This is 1st lineCurrent Position: 28
6. truncate ( )
The truncate( ) method truncates the file‘s size. It resizes the file stream to size bytes.
If size is not specified, it resizes to current location. The size defaults to the current
position and they remain unchanged.
Syntax
fileObject.truncate ( [size] )
size : It is an optional argument if present, the file is truncated to the given size
Program
f = open("demofile2.txt", "a")f.truncate(20)
f.close()
#open and read the file after the truncate:
f = open("demofile2.txt", "r")print(f.read())
Output
Hello! Welcome to py
Program
with open(―File.txt‖,‖r‖) as file:
line = file.readline()
word = line.split()
print(words)
Output
[‗Hello‘, ‗World‘, ‗Welcome‘, ‗to‘ , ‗the‘
‗world‘,‘of‘,‘Python‘,‘Programming‘]
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
>>> fout.write ('%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
>>> fout.write('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:
>>> fout.write ( '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:
>>> fout.write ( '%d %d %d' % (1, 2)
TypeError: not enough arguments for format string
>>> fout.write ('%d' % 'dollars')
TypeError: illegal argument type for built-in operation
The rename() Method: The rename() method takes two arguments, the current filename
and the new filename.
Its syntax is:
os.rename(old_file_name,new_file_name)
Program
import os
os.rename(“ File1.txt”, “ Students.txt”)
print (“ File Renamed”)
Output
File Renamed
The remove() Method: This method can be used to delete file(s). The method takes a
filename (name of the file to be deleted) as an argument and deletes that file.
Its syntax is:
os.remove (file_name)
Program
import os
os.remove(“Sudents.txt”)
print (“ File Deleted”)
Output
File Deleted
import sys
print(―the file name is %s‖ %(sys.argv[0]))
Output:
python demo.py
the file name is demo.py
Example 2- addition of two num
import sys
a= sys.argv[1]
b= sys.argv[2]
sum=int(a)+int(b)
print("sum is",sum)
Output:
sam@sam~$ python sum.py 2 3
sum is 5
MODULES
Modules are pre-written piece of code that are used to perform common task like
performing mathematical operation, generating random number. Modules are used to
break down complex programs into small manageable andorganized files
Example:
def greeting(name):
print("Hello, " + name)
2. Use a Module: To use the module , the import statement is used in the pythonscript
where it is needed.
Example:
#Import the module named mymodule, and the greeting function is called
import mymodule
mymodule.greeting("Akash")
Output:
Hello, Akash
Note:The syntax to use a function from a module
module_name.function_name.
IMPORT STATEMENT
All the variables and function in a module can be used into another program, but to use
only selected function or variable in a module from import statement is used
Example:
from math import pi,sqrt
print(pi)
print(sqrt(25))
Output:
3.14159265359
5.0
Example:
from math import *
a=25
print(pi)
print(sqrt(a))
Output:
3.14159265359
5.0
Output:
5.0
Variables in Module:
The module contains functions, and also variables of all types (arrays, dictionaries,
objects etc):
Example:
import mymodule
a = mymodule.person1["age"]
print(a)
Output:
36
dirfunction:
dir( ) is a built-in function that lists the identifiers defined in a module. These identifiers
may include functions, classes and variables.
If no name is specified, the dir() will return the list of names defined in the current
module.
Example:
import math
x = dir(math)
print(x)
Output:
[' doc ', ' name ', ' package ', 'acos', 'acosh',
'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil',
'copysign', 'cos', 'cosh', 'degrees', 'e', 'exp', 'fabs',
'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'hypot',
'isinf', 'isnan', 'ldexp', 'log', 'log10', 'log1p',
'modf', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt',
'tan', 'tanh', 'trunc']
BUILT-IN MODULES IN PYTHON
1. math — Mathematical functions
This module provides access to the mathematical functions defined by the Cstandard.
These functions cannot be used with complex numbers; use the functions of the same
name from the cmath module to support for complex numbers.
The following are some functions provided by this module. Except when explicitly
noted otherwise, all return values are floats.
The datetime module supplies classes for manipulating dates and times.
While date and time arithmetic is supported, the focus of the implementation is on
efficient attribute extraction for output formatting and manipulation.
>>>random.randrange(88)17
2. random.randrange(start,stop[,step]) : Return a randomly selected integer
element from range (start, stop, step).
>>>random.randrange(3,100,5)
83
3. random.choice(seq) :Return a random element from the non-empty sequence seq.
If seq is empty, raises IndexError.
>>>random.choice('abcdefghij')' e '
>>>random.choice(['aa','bb','cc',11,22])' cc'
PACKAGES
mod1.py
def a():
print('Hello')
mod2.py
def b():
print('Everyone')
Output:
Hello
Everyone
Locating modules :
When a module is imported, the Python interpreter searches for the modulein the
following sequences :
1. The current directory
2. If the module is not found, Python then searches each directory in the shell
variable PYTHONPATH environment variable (a list of directory
names, with the same syntax as the shell variable PATH).
3. If all else fails, Python checks the default path.
Errors and Exceptions
Errors are the problems in a program due to which the program will stop theexecution.
On the other hand, exceptions are raised when some internal events occur which
changes the normal flow of the program.
1. Syntax errors
2. Logical errors
Syntax errors
Syntax errors are detected when the rules of the particular programming language
are not followed while writing a program.
These errors are also known as parsing errors.
On encountering a syntax error, the Python interpreter does not execute the program
unless the errors are rectified.
When a syntax error is encountered while working in shell mode, Python displays
the name of the error and a small description about the error as shown in Example.
Example
if(amount>2999)
print("You are eligible to purchase ")
Output:
It returns a syntax error message because after in if statement a colon : is missing. This
can be fixed by writing the correct syntax.
Logical errors
Logic error specifies all those type of errors in which the program executes but gives
incorrect results.
particular program
dividedby zero then ZeroDivisionError exception is
raised, or when a module is imported that does not exist then ImportError is raised.
-time error that causes the program to
terminate abruptly. These types of run-time errors are known as exceptions.
Example :
# initialize the amount variable
marks = 10000
Output:
Exception Handling
Exception handling is a useful technique that helps in capturing runtime errors and
handling them so as to avoid the program getting crashed.
Python categorizes exceptions into distinct types so that specific exception handlers
(code to handle that particular exception) can be created for each type.
Exception handlers separate the main logic of the program from the error detection
and correction code.
The segment of code where there is any possibility of error or exception, is placed
inside one block. The code to be executed in case the exception has occurred, is
placed inside another block.
The compiler or interpreter keeps track of the exact position where the error has
occurred.
Exception handling can be done for both user-defined and built-inexceptions.
i) Handling Exceptions with try/except
try:
statements
except ExceptionName:
statements
Example :
try:
numerator=50
denom=int(input("Enter the denominator"))
quotient=(numerator/denom)
print ("Division performed successfully")
except ZeroDivisionError:
print ("Denominator as ZERO.... not allowed")
print(―OUTSIDE try..except block‖)
Output
Sometimes, a single piece of code might be suspected to have more than one type
of error.
For handling such situations, Python allows multiple except blocks for a single try
block.
The block which matches with the exception generated will get executed.
A try block can be associated with more than one except block to specify handlers
for different exceptions. However, only one handler will be executed.
Exception handlers only handle exceptions that occur in the corresponding try
block.
The syntax for specifying multiple except blocks for a single try block,
try:
statements
except Exception1:
If there is Exception1, then execute this block
except Exception2:
If there is Exception2, then execute this block
else :
If there is no Exception, then execute this
block
Example
try:
numerator=50
denom=int(input("Enter the denominator: "))
print (numerator/denom)
print ("Division performed successfully")
except ZeroDivisionError:
print ("Denominator as ZERO is not allowed")
except ValueError:
print ("Only INTEGERS should be entered")
When code in except block is the same for multiple exceptions being caught then
multiple exceptions can be handled in a single except block.
Example
try:
num=int(input(“Enter the number:”))
print (num**2)
except (ValueError,TypeError, KeyboardInterrupt):
print(“Please check before you enter… Prog
Terminating”)
print(„outside try block‟)
Output
Example:
try:
numerator=50
denom=int(input("Enter the denominator"))
quotient=(numerator/denom)
print ("Division performed successfully")
except ValueError:
print ("Only INTEGERS should be entered")
except:
print(" OOPS.....SOME EXCEPTION RAISED")
v. try...except…else clause
The try ... except block can optionally have an else clause, which,when present, must
follow all except blocks.
The statement(s) in the else block is executed only if the try clause does not raise an
exception.
Syntax
try:
# Some Code
except:
# Executed if error in the
# try block
else:
# execute if no exception
Example
try:
file=open(‗File1.txt‘)str=file.readline()print(str)
except IOError:
print(―Error occurred during input…‖)
else:
print(―Program executed successfully‖)
Output 1 Output 2
The try statement in Python can also have an optional finally clause.
The statements inside the finally block is always executed regardless of whether an
exception has occurred in the try block or not.
It is a common practice to use finally clause while working with files to ensure that
the file object is closed.
If used, finally should always be placed at the end of try clause, after all except
blocks and the else block irrespective of whether an exception has occurred or not.
The syntax of finally block can be given as,
try:
Write your operations here
......................
Due to any exception, operations written here
will be skipped
finally:
This would always be executed.
......................
Example
try:
numerator=50
denom=int(input("Enter the denominator: "))
quotient=(numerator/denom)
print ("Division performed successfully")
except ZeroDivisionError:
print ("Denominator as ZERO is not allowed")
except ValueError:
print ("Only INTEGERS should be entered")
else:
print ("The result of division operation is ",quotient)
finally:
print ("In finally block")
Output 1 Output 2
Enter the denominator:2Division Enter the denominator: 0 Denominator
performed successfully as ZERO is notallowed
The result of divisionoperation is 25 In finally block
In finally block
try:
def divide(num,deno):
try:
Quo=num/deno
except ZeroDivisionError:
print(“Cannot divide by a number by zero”)
divide(10,0)
Output
Cannot divide by a number by zero
Raising Exceptions
Programmers can also forcefully raise exceptions in a program using the raise and
assert statements.
Once an exception is raised, no further statement in the current block of code is
executed.
So, raising an exception involves interrupting the normal flow execution of program
and jumping to that part of the program (exception handler code) which is written
to handle such exceptionalsituations.
1. raise statement
Example
try:
num=10
print(num)
raise ValueError
except:
print(“Execution occurred….Prog terminating”)
Output
10
Execution occurred….Prog terminating
assert Expression[,arguments]
Example
Output
10000
Negative Number