Python Tutorial

File And Exceptions:

This page can be downloaded as interactive jupyter notebook

An additional quiz about the contents of this tutorial, can be downloaded here (with solutions)


File is a location in our computer hard disk which is named to store information.

In python, when we want to read from or write to a file, we need first to open it. After any edition, we have to close the file. Hence, operating a file takes place in the following order:

1- Open file

2- Read and write (Edit)

3- Close the file

Open a file

In python, we can open a file using a built-in function open(). In the following example, you can see two different types of this function.

Also, we can specify the mode of the built-in function open() using r to read, w to write or a to append to the file. And we can specify to open the file in text or binary mode.

Example: Different type of opening a file.

# Open a file in current directory
A = open("my_data.txt")

# Open a file specifying full path
B = open("D:/PythonTutorial/my_file.txt")

# Read in text mode
C = open("my_data.txt",'r')

# Write in binary mode
D = open("my_data.txt",'w+b')

The following table represents Python file modes.

Mode Description
'r' It is default mode to open a file for reading.
'w' Open a file for writing.It truncates the file if it exists If not, it creates a new file.
'x' Open a file for exclusive creation. The operation fails, if the file already exists.
'a' Open the file for appending. The operation creates a new file, if it does not exist.
't' Open a file in text mode.
'b' Open a file in binary mode.
'+' Open a file for reading and writing.

Note: In python, the default encoding is platform dependent. In Linux, it is utf-8 and in Windows, it is cp1252. Therefore, we must not rely on the default encoding, because our code will behave differently in different platform. Then, we have to specify the encoding type, when we are working with a file in text mode.

Example: Open a file in text mode and specify the encoding type.

# Read in text mode
C = open("my_data.txt",mode = 'r', encoding = 'utf-8')

Close a file

We have to close the file,when we have done the operations to the file. In python, closing the file can be done using close() method.

Example: Closing a file.

# Read in text mode
d = open("my_data.txt",encoding = 'utf-8')

# Close the file

Unfortunately, this method is not safe. When, we are performing some operations with a file, if an exception occurs, there would be a unclosed file.

We can guarantee that the file is closed, using a try...finally block.

Example: Closing a file by try...finally block.

    d = open("my_data.txt",encoding = 'utf-8')
    # Perform file operations here

The best way to open a file, perform operations and close the file is using the with statement. This statement ensures that the file is closed when the block inside with is exited.


with open("my_data.txt",encoding = 'utf-8') as f:
    # Perform file operations here

Write into a file

In python, we need to open the file in write w, append a or exclusive creation x mode in order to write into a file.

Note: The write mode overwrites into the file if it exists.

Example: Writing into a file.

with open("my_data.txt",'w',encoding = 'utf-8') as f:
    f.write("Creating first file\n")
    f.write("second line of the file\n")
    f.write("Third line of the file\n\n")
    f.write("Last line writing in this file\n")

Read a file

In python, we have to open a file in read mode to read it. We can use read(size) method to read a file in size number of data. If the size is not specified, method reads up to the end of the file.

Example: Reading a file.

# Open the file in read mode
f = open("my_data.txt",'r',encoding = 'utf-8')

# Read the five three data
# Read up to end of the file
ing first file
second line of the file
Third line of the file

Last line writing in this file
  • We can read a file line-by-line using a for loop.

Example: Reading a file line-by-line.

p = open("my_data.txt",'r',encoding = 'utf-8')

for line in p:
    print(line, end = '')
Creating first file

second line of the file

Third line of the file

Last line writing in this file

We can read individual lines from a file using readline() method. This method reads the file till first line finished.

Example: Read individual lines from a file.

# Open the file in read mode
q = open("my_data.txt",'r',encoding = 'utf-8')

# Read first line
'Creating first file\n'
# Read second line
'second line of the file\n'
# Open the file in read mode
b = open("my_data.txt",'r',encoding = 'utf-8')

# Read all line
['Creating first file\n',
 'second line of the file\n',
 'Third line of the file\n',
 'Last line writing in this file\n']

File Methods

Python has various file object methods. Here is a list of useful methods in text mode.

Method Description
1 read(n) It reads n characters from the file. If n is negative or None, it reads till end of the file.
2 readable() If the file tream is readable, it returns True.
3 readline(n=-1) It reads amd returns one line from the file. If n is specified, it reads n bytes.
4 readlines(n=-1) It reads and returns a list of lines from the file. If n is specified, it reads at most n bytes/characters.
5 write(s) It writes s to the file and returns the number of characters written.
6 writable() If the file stream is available to write, it returns True.
7 writelines(lines) It writes a list of lines into a file.
8 detach() Separate the underlying binary buffer from the TextIOBase and return it.
9 truncate(size=None) It resizes the file stream to the specified size. It resizes to current location, if the size is not specified.
10 tell() It returns the current file location.
11 fileno() It returns a file descriptor (an integer number) of the file.
12 flush() It flushs the write buffer of the file stream.
13 seek(offset,from=SEEK_SET) Change the file position to offset bytes, in reference to from (start, current, end).
14 seekable() If the file stream supports random access, it returns True.
15 isatty() If the file stream is iteractive, it returns True.
16 close() Close an open file. It has no effect if the file is already closed.

Errors and Exceptions

In python, we have two kind of errors: syntax errors and exceptions.

Syntax errors

Syntax error is most common error which complaint you when you are learning programming with Python. It is also known as parsing error.

Syntax errors happen, when the program is not following the proper structure of the language.

When a syntax error happens, the parser repeats the line and displays a short explanation (starts with Syntax error:) at the point where the error occurs.

Example: Please run the code below to see how a syntax error occurs.

if True
print("It is True")
  File "<ipython-input-1-fb874d49caa0>", line 1
    if True
SyntaxError: invalid syntax

The correct form of the code is in the following:

if True:
    print("It is true")
It is true


Exceptions are the errors which are detected during execution. The exceptions may cause an error when a try is made to execute the statement or expression, even the statement or expression is syntactically correct. Most exceptions are not handled by programs, then we have to handle them in python programs.

Example: Let’s run the codes below to see how the exceptions occurs.

5 + 2*A

NameError                                 Traceback (most recent call last)

<ipython-input-3-f7600da9b53c> in <module>()
----> 1 5 + 2*A

NameError: name 'A' is not defined

ZeroDivisionError                         Traceback (most recent call last)

<ipython-input-4-700aace389cf> in <module>()
----> 1 6/0

ZeroDivisionError: division by zero
'4' + 6

TypeError                                 Traceback (most recent call last)

<ipython-input-6-d61313780835> in <module>()
----> 1 '4' + 6

TypeError: must be str, not int

There is different types of Exceptions which are printed as a part of the error message by red letters (NameError). The exception type which is printed as a string is the name of the built-in exception that occurred. The error message represents what happened.

In the following list, some of the common built-in exceptions in python programming are tabulated.

Exception Cause of Error
1 AssertionError It happends, when 'assert' staement fails.
2 AttributeError It happens, when attribute assignment or reference fails.
3 EOFError It happens, when the 'input()' functions hits end-of-file condition.
4 FloatingPointError It happens, a floating point operation fails.
5 GeneratorExit It happens, when a generator's 'close()' method is called.
6 ImportError It happens, the imported module is not found.
7 IndexError It happens, when index of a sequence is out of range.
8 KeyError It happens, when a key is not found in a dictionary.
9 KeyboardInterrupt It happens, when the user hits interrupt key (Ctrl+c or delete).
10 MemoryError It happens, when an operation runs out of memory.
11 NameError It happens, when a variable is not found in local or global scope.
12 NotImplementedError It happens by abstract methods.
13 OSError It happens, when system operation causes system related error.
14 OverflowError It happens, when result of an arithmetic operation is too large to be represented.
15 ReferenceError It happens, when a weak reference proxy is used to access a garbage collected referent.
16 RuntimeError It happens, when an error does not fall under any other category.
17 StopIteration It happens by 'next()' function to indicate that there is no further item to be returned by iterator.
18 SyntaxError It happens by parser when syntax error is encountered.
19 IndentationError It happens, when there is incorrect indentation.
20 TabError It happens, when indentation consists of inconsistent tabs and spaces.
21 SystemError It happens, when interpreter detects internal error.
22 SystemExit It happens by 'sys.exit()' function.
23 TypeError It happens, when a function or operation is applied to an object of incorrect type.
24 UnboundLocalError It happens, when a reference is made to a local variable in a function or method, but no value has been bound to that variable.
25 UnicodeError It happens, when a Unicode-related encoding or decoding error occurs.
26 UnicodeEncodeError It happens, when a Unicode-related error occurs during encoding.
27 UnicodeDecodeError It happens, when a Unicode-related error occurs during decoding.
28 UnicodeTranslateError It happens, when a Unicode-related error occurs during translating.
29 ValueError It happens, when a function gets argument of correct type but improper value.
30 ZeroDivisionError It happens, when second operand of division or modulo operation is zero.

Handling Exceptions

In python, it is possible to write programs which can handle exceptions. A try statement can handle exceptions. In the following example:

First, the try clause (the statement(s) between the try and except keyword) is executed.

  • If no exceptions occurs, the except clause is skipped and execution of the try statement is finished.

  • If during the try clause execution an exception occurs, the rest of the clause skipped. Then, if its type matches the exception named after the except keyword, the except clause is executed, and then after the try statement the execution continues.

  • If in the except clause, an exception occurs which does not match the exception named, it is passed on to outer try statement. If no handler is found, it is an unhandled exception and execution stops with a message.

# A while loop with try statement
while True:
        # try clause
        v = int(input("Please enter a number: "))
    except ValueError:
        print("Oops! It is not a valid number. Enter another number...")
Please enter a number: 5.3
Oops! It is not a valid number. Enter another number...
Please enter a number: 6.1
Oops! It is not a valid number. Enter another number...
Please enter a number: 2.0
Oops! It is not a valid number. Enter another number...
Please enter a number: 4

Here, the loop will continue until the user enters an integer value. If the mentioned exception ValueError does not occur, the exception block is skipped and normal flow continues.

Also, it is possible to handle more than one exception in a try statement. It might have more than one except clause for different exceptions.

Example: Handling more than one exception.

# Define some classes
class A(Exception):

class B(A):

# loop
for i in [A, B]:
        raise i()
    except B:
    except A:

Note: The raise statement allows us to force an exception to occur.

Author: Mohsen Soleymanighezelgechi
Last modified: 25.10.2019