MC4103 Python Programming - Unit-Iii
MC4103 Python Programming - Unit-Iii
Files: Introduction – File Path – Opening and Closing Files – Reading and Writing Files –File
Position –Exception: Errors and Exceptions, Exception Handling, Multiple Exceptions
Python - Files I/O
This chapter covers all the basic I/O functions available in Python. For more functions, please
refer to standard Python documentation.
The simplest way to produce output is using the print statement where you can pass zero or more
expressions separated by commas. This function converts the expressions you pass into a string
and writes the result to standard output as follows −
#!/usr/bin/python
Python provides two built-in functions to read a line of text from standard input, which by
default comes from the keyboard. These functions are −
raw_input
input
The raw_input([prompt]) function reads one line from standard input and returns it as a string
(removing the trailing newline).
#!/usr/bin/python
This prompts you to enter any string and it would display same string on the screen. When I
typed "Hello Python!", its output is like this −
The input([prompt]) function is equivalent to raw_input, except that it assumes the input is a
valid Python expression and returns the evaluated result to you.
#!/usr/bin/python
This would produce the following result against the entered input −
Until now, you have been reading and writing to the standard input and output. Now, we will see
how to use actual data files.
Python provides basic functions and methods necessary to manipulate files by default. You can
do most of the file manipulation using a file object.
Before you can read or write a file, you have to open it using Python's built-in open() function.
This function creates a file object, which would be utilized to call other support methods
associated with it.
Syntax
file_name − The file_name argument is a string value that contains the name of the file
that you want to access.
access_mode − The access_mode determines the mode in which the file has to be
opened, i.e., read, write, append, etc. A complete list of possible values is given below in
the table. This is optional parameter and the default file access mode is read (r).
buffering − If the buffering value is set to 0, no buffering takes place. If the buffering
value is 1, line buffering is performed while accessing a file. If you specify the buffering
value as an integer greater than 1, then buffering action is performed with the indicated
buffer size. If negative, the buffer size is the system default(default behavior).
9 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
10 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.
11 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+
12 Opens a file for both appending and reading in binary format. 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.
Once a file is opened and you have one file object, you can get various information related to that
file.
Example
#!/usr/bin/python
# Open a file
fo = open("foo.txt", "wb")
print "Name of the file: ", fo.name
print "Closed or not : ", fo.closed
print "Opening mode : ", fo.mode
print "Softspace flag : ", fo.softspace
The close() method of a file object flushes any unwritten information and closes the file object,
after which no more writing can be done.
Python automatically closes a file when the reference object of a file is reassigned to another file.
It is a good practice to use the close() method to close a file.
Syntax
fileObject.close()
Example
#!/usr/bin/python
# Open a file
fo = open("foo.txt", "wb")
print "Name of the file: ", fo.name
The file object provides a set of access methods to make our lives easier. We would see how to
use read() and write() methods to read and write files.
The write() method writes any string to an open file. It is important to note that Python strings
can have binary data and not just text.
The write() method does not add a newline character ('\n') to the end of the string −
Syntax
fileObject.write(string)
Here, passed parameter is the content to be written into the opened file.
Example
#!/usr/bin/python
# Open a file
fo = open("foo.txt", "wb")
fo.write( "Python is a great language.\nYeah its great!!\n")
The above method would create foo.txt file and would write given content in that file and finally
it would close that file. If you would open this file, it would have following content.
The read() method reads a string from an open file. It is important to note that Python strings can
have binary data. apart from text data.
Syntax
fileObject.read([count])
Here, passed parameter is the number of bytes to be read from the opened file. This method starts
reading from the beginning of the file and if count is missing, then it tries to read as much as
possible, maybe until the end of file.
Example
#!/usr/bin/python
# Open a file
fo = open("foo.txt", "r+")
str = fo.read(10);
print "Read String is : ", str
# Close opend file
fo.close()
File Positions
The tell() method tells you the current position within the file; in other words, the next read or
write will occur at that many bytes from the beginning of the file.
The seek(offset[, from]) method changes the current file position. The offset argument indicates
the number of bytes to be moved. The from argument specifies the 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.
Example
#!/usr/bin/python
# Open a file
fo = open("foo.txt", "r+")
str = fo.read(10)
print "Read String is : ", str
To use this module you need to import it first and then you can call any related functions.
The rename() method takes two arguments, the current filename and the new filename.
Syntax
os.rename(current_file_name, new_file_name)
Example
#!/usr/bin/python
import os
You can use the remove() method to delete files by supplying the name of the file to be deleted
as the argument.
Syntax
os.remove(file_name)
Example
#!/usr/bin/python
import os
Directories in Python
All files are contained within various directories, and Python has no problem handling these too.
The os module has several methods that help you create, remove, and change directories.
You can use the mkdir() method of the os module to create directories in the current directory.
You need to supply an argument to this method which contains the name of the directory to be
created.
Syntax
os.mkdir("newdir")
Example
#!/usr/bin/python
import os
You can use the chdir() method to change the current directory. The chdir() method takes an
argument, which is the name of the directory that you want to make the current directory.
Syntax
os.chdir("newdir")
Example
#!/usr/bin/python
import os
os.getcwd()
Example
#!/usr/bin/python
import os
The rmdir() method deletes the directory, which is passed as an argument in the method.
Syntax
os.rmdir('dirname')
Example
Following is the example to remove "/tmp/test" directory. It is required to give fully qualified
name of the directory, otherwise it would search for that directory in the current directory.
#!/usr/bin/python
import os
There are three important sources, which provide a wide range of utility methods to handle and
manipulate files & directories on Windows and Unix operating systems. They are as follows −
File Object Methods: The file object provides functions to manipulate files.
OS Object Methods: This provides methods to process files as well as directories.
Example: Error
>>> print "hello"
SyntaxError: Missing parentheses in call to 'print'. Did you mean print("hello")?
In Python 3.x, print is a built-in function and requires parentheses. The statement above violates
this usage and hence syntax error is displayed.
Many times though, a program results in an error after it is run even if it doesn't have any syntax
error. Such an error is a runtime error, called an exception. A number of built-in exceptions are
defined in the Python library. Let's see some common error types.
Exception Description
AssertionError Raised when the assert statement fails.
AttributeError Raised on the attribute assignment or reference fails.
EOFError Raised when the input() function hits the end-of-file condition.
FloatingPointError Raised when a floating point operation fails.
GeneratorExit Raised when a generator's close() method is called.
ImportError Raised when the imported module is not found.
IndexError Raised when the index of a sequence is out of range.
KeyError Raised when a key is not found in a dictionary.
KeyboardInterrupt Raised when the user hits the interrupt key (Ctrl+c or delete).
MemoryError Raised when an operation runs out of memory.
NameError Raised when a variable is not found in the local or global scope.
NotImplementedError Raised by abstract methods.
OSError Raised when a system operation causes a system-related error.
Raised when the result of an arithmetic operation is too large to be
OverflowError
represented.
Raised when a weak reference proxy is used to access a garbage
ReferenceError
collected referent.
RuntimeError Raised when an error does not fall under any other category.
Raised by the next() function to indicate that there is no further item to
StopIteration
be returned by the iterator.
SyntaxError Raised by the parser when a syntax error is encountered.
IndentationError Raised when there is an incorrect indentation.
TabError Raised when the indentation consists of inconsistent tabs and spaces.
SystemError Raised when the interpreter detects internal error.
SystemExit Raised by the sys.exit() function.
TypeError Raised when a function or operation is applied to an object of an
Exception Description
incorrect type.
Raised when a reference is made to a local variable in a function or
UnboundLocalError
method, but no value has been bound to that variable.
UnicodeError Raised when a Unicode-related encoding or decoding error occurs.
UnicodeEncodeError Raised when a Unicode-related error occurs during encoding.
UnicodeDecodeError Raised when a Unicode-related error occurs during decoding.
UnicodeTranslateError Raised when a Unicode-related error occurs during translation.
Raised when a function gets an argument of correct type but improper
ValueError
value.
Raised when the second operand of a division or module operation is
ZeroDivisionError
zero.
IndexError
Example: IndexError
>>> L1=[1,2,3]
>>> L1[3]
Traceback (most recent call last):
File "<pyshell#18>", line 1, in <module>
L1[3]
IndexError: list index out of range
ModuleNotFoundError
Example: ModuleNotFoundError
>>> import notamodule
Traceback (most recent call last):
File "<pyshell#10>", line 1, in <module>
import notamodule
ModuleNotFoundError: No module named 'notamodule'
KeyError
Example: KeyError
>>> D1={'1':"aa", '2':"bb", '3':"cc"}
>>> D1['4']
Traceback (most recent call last):
File "<pyshell#15>", line 1, in <module>
D1['4']
KeyError: '4'
ImportError
Example: ImportError
>>> from math import cube
Traceback (most recent call last):
File "<pyshell#16>", line 1, in <module>
StopIteration
The StopIteration is thrown when the next() function goes beyond the iterator items.
Example: StopIteration
>>> it=iter([1,2,3])
>>> next(it)
1
>>> next(it)
2
>>> next(it)
3
>>> next(it)
Traceback (most recent call last):
File "<pyshell#23>", line 1, in <module>
next(it)
StopIteration
TypeError
Example: TypeError
>>> '2'+2
Traceback (most recent call last):
File "<pyshell#23>", line 1, in <module>
'2'+2
TypeError: must be str, not int
ValueError
Example: ValueError
>>> int('xyz')
Traceback (most recent call last):
File "<pyshell#14>", line 1, in <module>
int('xyz')
ValueError: invalid literal for int() with base 10: 'xyz'
NameError
Example: NameError
>>> age
Traceback (most recent call last):
File "<pyshell#6>", line 1, in <module>
age
NameError: name 'age' is not defined
ZeroDivisionError
The ZeroDivisionError is thrown when the second operator in the division is zero.
Example: ZeroDivisionError
>>> x=100/0
Traceback (most recent call last):
File "<pyshell#8>", line 1, in <module>
x=100/0
ZeroDivisionError: division by zero
KeyboardInterrupt
The KeyboardInterrupt is thrown when the user hits the interrupt key (normally Control-C)
during the execution of the program.
Example: KeyboardInterrupt
>>> name=input('enter your name')
enter your name^c
Traceback (most recent call last):
File "<pyshell#9>", line 1, in <module>
Python provides two very important features to handle any unexpected error in your Python
programs and to add debugging capabilities in them −
Exception Handling − This would be covered in this tutorial. Here is a list standard
Exceptions available in Python: Standard Exceptions.
Assertions − This would be covered in Assertions in Python tutorial.
Assertions in Python
An assertion is a sanity-check that you can turn on or turn off when you are done with your
testing of the program.
The easiest way to think of an assertion is to liken it to a raise-if statement (or to be more
accurate, a raise-if-not statement). An expression is tested, and if the result comes up false, an
exception is raised.
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.
When it encounters an assert statement, Python evaluates the accompanying expression, which is
hopefully true. If the expression is false, Python raises an AssertionError exception.
If the assertion fails, Python uses ArgumentExpression as the argument for the AssertionError.
AssertionError exceptions can be caught and handled like any other exception using the try-
except statement, but if not handled, they will terminate the program and produce a traceback.
Example
Here is a function that converts a temperature from degrees Kelvin to degrees Fahrenheit. Since
zero degrees Kelvin is as cold as it gets, the function bails out if it sees a negative temperature −
Live Demo
#!/usr/bin/python
def KelvinToFahrenheit(Temperature):
assert (Temperature >= 0),"Colder than absolute zero!"
return ((Temperature-273)*1.8)+32
print KelvinToFahrenheit(273)
print int(KelvinToFahrenheit(505.78))
print KelvinToFahrenheit(-5)
32.0
451
Traceback (most recent call last):
File "test.py", line 9, in <module>
print KelvinToFahrenheit(-5)
File "test.py", line 4, in KelvinToFahrenheit
assert (Temperature >= 0),"Colder than absolute zero!"
AssertionError: Colder than absolute zero!
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.
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.
Syntax
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 and comes out gracefully because there is no
problem at all −
Live Demo
#!/usr/bin/python
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()
Example
This example tries to open a file where you do not have write permission, so it raises an
exception −
Live Demo
#!/usr/bin/python
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"
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.
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
Live Demo
#!/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 −
#!/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 are 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
Live Demo
#!/usr/bin/python
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
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 −
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
Multiple Exception Handling in Python
Given a piece of code that can throw any of several different exceptions, and one needs to
account for all of the potential exceptions that could be raised without creating duplicate code or
long, meandering code passages.
If you can handle different exceptions all using a single block of code, they can be grouped
together in a tuple as shown in the code given below :
Code #1 :
try:
client_obj.get_url(url)
except (URLError, ValueError,
SocketTimeout):
client_obj.remove_url(url)
The remove_url() method will be called if any of the listed exceptions occurs. If, on the other
hand, if one of the exceptions has to be handled differently, then put it into its own except clause
as shown in the code given below :
Code #2 :
try:
client_obj.get_url(url)
except (URLError, ValueError):
client_obj.remove_url(url)
except SocketTimeout:
client_obj.handle_url_timeout(url)
Many exceptions are grouped into an inheritance hierarchy. For such exceptions, all of the
exceptions can be caught by simply specifying a base class. For example, instead of writing code
as shown in the code given below –
Code #3 :
try:
f = open(filename)
except (FileNotFoundError, PermissionError):
...
Except statement can be re-written as in the code given below. This works because OSError is a
base class that’s common to both the FileNotFoundError and PermissionError exceptions.
Code #4 :
try:
f = open(filename)
except OSError:
...
Although it’s not specific to handle multiple exceptions per se, it is worth noting that one can get
a handle to the thrown exception using them as a keyword as shown in the code given below.
Code #5 :
try:
f = open(filename)
except OSError as e:
if e.errno == errno.ENOENT:
logger.error('File not found')
elif e.errno == errno.EACCES:
logger.error('Permission denied')
else:
logger.error('Unexpected error: % d',
e.errno)
The e variable holds an instance of the raised OSError. This is useful if the exception has to be
invested further, such as processing it based on the value of the additional status code. The
except clauses are checked in the order listed and the first match executes.
f = open('missing')
Output :
Failed
Here the except FileNotFoundError clause doesn’t execute because the OSError is more general,
matches the FileNotFoundError exception, and was listed first.