I/O and Error Handling In Python
An exception in Python is an incident that happens while executing a program that causes the
regular course of the program's commands to be disrupted. When a Python code comes across a
condition it can't handle, it raises an exception. An object in Python that describes an error is called
an exception.
When a Python code throws an exception, it has two options: handle the exception immediately or
stop and quit. When the interpreter identifies a statement that has an error, syntax errors occur.
Consider the following scenario:
string = "Python Exceptions"
for s in string:
if (s != o: ----------- invalid syntax ) is missing.
print( s )
For each try block, there can be zero or more except blocks. Multiple except blocks allow
us to handle each exception differently.
The argument type of each except block indicates the type of exception that can be handled
by it. For example 1,
try:
even_numbers = [2,4,6,8]
print(even_numbers[5])
except ZeroDivisionError:
print("Denominator cannot be 0.")
except IndexError:
print("Index Out of Bound.")
# Output: Index Out of Bound
2. Example - program to print the reciprocal of even numbers
try:
num = int(input("Enter a number: "))
assert num % 2 == 0
except:
print("Not an even number!")
else:
reciprocal = 1/num
print(reciprocal)
Data Stream
A data stream is an existing, continuous, ordered (implicitly by entrance time or explicitly by
timestamp) chain of items. As we all know, big data primarily focuses on data storage, which incurs
significant costs, and storage has the tendency to make data unstructured. This is not beneficial to
machine learning algorithms. A data stream is a series of digitally encoded coherent signals that are
used to transfer data during the data transfer process. A data stream is a collection of information
extracted from the receiver and provided by the data provider.
Examples of Stream Sources-
1. Sensor Data –
In navigation systems, sensor data is used. Imagine a temperature sensor floating
about in the ocean, sending back to the base station a reading of the surface
temperature each hour. The data generated by this sensor is a stream of real numbers.
We have 3.5 terabytes arriving every day and we for sure need to think about what we
can be kept continuing and what can only be archived.
2. Image Data –
Satellites frequently send down-to-earth streams containing many terabytes of images
per day. Surveillance cameras generate images with lower resolution than satellites,
but there can be numerous of them, each producing a stream of images at a break of 1
second each.
3. Internet and Web Traffic –
A bobbing node in the center of the internet receives streams of IP packets from many
inputs and paths them to its outputs. Websites receive streams of heterogeneous types.
For example, Google receives a hundred million search queries per day.
Characteristics of Data Streams :
1. Large volumes of continuous data, possibly infinite.
2. Steady changing and requires a fast, real-time response.
3. Data stream captures nicely our data processing needs of today.
4. Random access is expensive and a single scan algorithm
5. Store only the summary of the data seen so far.
6. Maximum stream data are at a pretty low level or multidimensional in creation, needs
multilevel and multidimensional treatment.
Access Modes
Access modes govern the type of operations possible in the opened file. It refers to how the file will
be used once its opened. These modes also define the location of the File Handle in the file. File
handle is like a cursor, which defines from where the data has to be read or written in the file. There
are 6 access modes in python.
1. Read Only ('r’): This mode opens the text files for reading only. The start of the file
is where the handle is located. It raises the I/O error if the file does not exist. This is
the default mode for opening files as well.
2. Read and Write ('r+’): This method opens the file for both reading and writing. The
start of the file is where the handle is located. If the file does not exist, an I/O error
gets raised.
3. Write Only ('w’): This mode opens the file for writing only. The data in existing files
are modified and overwritten. The start of the file is where the handle is located. If the
file does not already exist in the folder, a new one gets created.
4. Write and Read ('w+’): This mode opens the file for both reading and writing. The
text is overwritten and deleted from an existing file. The start of the file is where the
handle is located.
5. Append Only ('a’): This mode allows the file to be opened for writing. If the file
doesn't yet exist, a new one gets created. The handle is set at the end of the file. The
newly written data will be added at the end, following the previously written data.
6. Append and Read (‘a+’): Using this method, you can read and write in the file. If
the file doesn't already exist, one gets created. The handle is set at the end of the file.
The newly written text will be added at the end, following the previously written data.
f = open("[Link]", "r")
#('r’) opens the text files for reading only
print([Link]())
#The "[Link]" prints out the data in the text file in the shell when run.
Writing Data to a File
To write to an existing file, you must add a parameter to the open() function:
"a" - Append - will append to the end of the file
"w" - Write - will overwrite any existing content
"x" - Create - will create a file, returns an error if the file exist
f = open("[Link]", "a")
[Link]("Now the file has more content!")
[Link]()
#open and read the file after the appending:
f = open("[Link]", "r")
print([Link]())
..............................................................
# open the file in write mode
myfile = open(“[Link]”,’w’)
[Link](“Hello World!”,”We’re learning Python!”)
# close the file
[Link]()
We can also write multiple lines to a file using special characters:
# open the file in write mode
myfile = open("[Link]", 'w')
line1 = "Roses are red.\n"
line2 = "Violets are blue.\n"
line3 = "Python is great.\n"
line4 = "And so are you.\n"
[Link](line1 + line2 + line3 + line4)
.........................................................
# Program to show various ways to read and
# write data in a file.
file1 = open("[Link]","w")
L = ["This is Delhi \n","This is Paris \n","This is London \n"]
# \n is placed to indicate EOL (End of Line)
[Link]("Hello \n")
[Link](L)
[Link]() #to change file access modes
file1 = open("[Link]","r+")
print("Output of Read function is ")
print([Link]())
print()
# seek(n) takes the file handle to the nth
# bite from the beginning.
[Link](0)
print( "Output of Readline function is ")
print([Link]())
print()
[Link](0)
# To show difference between read and readline
print("Output of Read(9) function is ")
print([Link](9))
print()
[Link](0)
print("Output of Readline(9) function is ")
print([Link](9))
[Link](0)
# readlines function
print("Output of Readlines function is ")
print([Link]())
print()
[Link]()
Reading Data From a File
read() : Returns the read bytes in form of a string. Reads n bytes, if no n specified, reads the entire
file.
File_object.read([n])
readline() : Reads a line of the file and returns in form of a [Link] specified n, reads at most n
bytes. However, does not reads more than one line, even if n exceeds the length of the line.
File_object.readline([n])
readlines() : Reads all the lines and return them as each line a string element in a list.
File_object.readlines()
Example 1.
# Define the name of the file to read from
filename = "[Link]"
# Open the file for reading
filehandle = open(filename, 'r')
while True:
# read a single line
line = [Link]()
if not line:
break
print(line)
# Close the pointer to that file
[Link]()
Example 2.
f = open("[Link]", "w")
[Link]("Woops! I have deleted the content!")
[Link]()
#open and read the file after the overwriting:
f = open("[Link]", "r")
print([Link]())
Additional File Methods
1. The flush() method is an inbuilt method in python. It is used to clear the internal
buffer when writing to file. It is the best method while working with file handling in
python.
Example:
myfile = open("[Link]", "a")
[Link]("Welcome to python")
[Link]()
[Link]("Programming.....")
To see the output, we will use “[Link]()” and it will clear the internal buffer of the file. But, it
does not affect the content of the file. So, the content of the file can be read and displayed.
2. Python file tell() method returns the current file position within the file. It does not take any
parameter and returns an integer value.
Example:
myfile = open("[Link]", "r")
print([Link]())
[Link]()
Here, when we will print “[Link]()” then the output will appear “ 0 ”. Here, initially file
pointer points to the beginning of the file if not opened in append mode. So, the initial value
of tell() is zero.
3. split() using file handling
We can also split lines using file handling in Python. This splits the variable when space is
encountered. You can also split using any characters as we wish. Here is the code:
# Python code to illustrate split() function
with open("[Link]", "r") as file:
data = [Link]()
for line in data:
word = [Link]()
print (word)
There are also various other functions that help to manipulate the files and their contents. One
can explore various other functions in Python Docs.
4. The readable() method in Python is an inbuilt method in python, it checks whether a
file is readable or not. This method returns a boolean value. The readable() method
returns True if the file is readable otherwise it will return False.
Example:
myfile = open("[Link]", "r")
print([Link]())
[Link]()
To get the output we will print “[Link]()” and it will return “True” as an
output because the file is readable. A file is readable if it’s opened using “r”.
Handling IO Exceptions
It is an error 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. It is also raised for operating
system-related errors.
If the given code is written in a try block, it raises an input/output exception, which is
handled in the except block as shown given below
Example
import sys
def whatever():
try:
f = open ( "[Link]", 'r' )
except IOError, e:
print e
print sys.exc_type
whatever()
Output
[Errno 2] No such file or directory: '[Link]'
<type '[Link]'>
Exception occurs in a program, the program gets terminated and an error message is displayed. This
behaviour is often undesirable especially when the exception is raised due to some external factor
(like due to some value entered by a user) and not due to some mistake in the code.
Errors, Run Time Errors
When working with Input and Output Operations in Python, if we encounter an error
related to file, the code will throw the IOError. When we attempt to open a file and if
it does not exist, the IOError will be encountered. In a case where the statement or the
line of code is correct, it may result in an error while execution. Now the error like
these, which are detected during the program execution are known as exceptions.
Commonly, the IOError is raised when an input output operation like open() file, or a
method or a simple print statement is failed due to IO reasons like “Disk full” or “File
not found”. The IOError class is inherited from the EnvironmentError.
When working with Input and Output Operations in Python, if we encounter an error
related to file, the code will throw the IOError. When we attempt to open a file and if
it does not exist, the IOError will be encountered. In a case where the statement or the
line of code is correct, it may result in an error while execution. Now the error like
these, which are detected during the program execution are known as exceptions.
Commonly, the IOError is raised when an input output operation like open() file, or a
method or a simple print statement is failed due to IO reasons like “Disk full” or “File
not found”. The IOError class is inherited from the EnvironmentError.
How IOError work in Python?
In a Python program, where we have a simple operation of print the content of a file,
we pass the file name and path of the file location.
But if the file that we passed, does not exist at the passed location or the file name has
been changed, then the operation we intend to execute won’t happen.
Which will result in an error related to Input Output, which is IOError.
So, basically, IOError is an exception type error that occurs when the file that we
passed in as argument, does not exist or as a different name or the file location path is
incorrect.
Any of these reason could raise an IOError.
There are many other errors that can be encountered and based on the requirement of
the code we have handle these error.
Exception Hierarchy
Exception handling occurs based on an exception hierarchy, determined by the inheritance
structure of the exception classes.
For example, IOError and OSError are both subclasses of EnvironmentError. Code that
catches an IOError will not catch an OSError. However, code that catches an
EnvironmentError will catch both IOErrors and OSErrors.
The hierarchy of built-in exceptions:
Python 2.x2.3
BaseException
+-- SystemExit
+-- KeyboardInterrupt
+-- GeneratorExit
+-- Exception
+-- StopIteration
+-- StandardError
| +-- BufferError
| +-- ArithmeticError
| | +-- FloatingPointError
| | +-- OverflowError
| | +-- ZeroDivisionError
| +-- AssertionError
| +-- AttributeError
| +-- EnvironmentError
| | +-- IOError
| | +-- OSError
| | +-- WindowsError (Windows)
| | +-- VMSError (VMS)
| +-- EOFError
| +-- ImportError
| +-- LookupError
| | +-- IndexError
| | +-- KeyError
Handling Multiple Exceptions
In Python, try-except blocks are used to catch exceptions and process them. Sometimes we
call a function that may throw multiple types of exceptions depending on the arguments,
processing logic, etc.
We handle different types of exceptions that might occur during the execution of the try
block. See the following Python program.
# Python catch multiple exceptions
try:
# defining variables
a = 10
b= 0
c = "abc"
# adding the variables
d =a+c
# Zerodivision error
except ZeroDivisionError:
print("Zero Division Error occurs")
# index error
except IndexError:
print("Index error occurs")
# type error
except TypeError:
print("Type error occurs")
Output:
Type error occurs
Type error occurs because in Python we cannot directly add string and integer data types
together. So because the type error was thrown by the try block, that is why only the
TypeError except block was executed.
We write all the exception types that might occur in a tuple and use one except block. See the
following syntax of multiple exceptions in one line.
try:
# Try statements
except(TypeError, SyntaxError, ValueError, ...)as e:
# exceptions