Unit 6 File Handling
Unit 6 File Handling
Prepared By:
Tanvi Patel
Asst. Prof. (CSE)
Contents
◉ Types of Files: Python file handling operations also known as Python I/O deal with two types of
files.
Types of Files (Cont.)
◉ Even though the two file types may look the same on the surface, they encode data differently.
◉ A text file is structured as a sequence of lines. And, each line of the text file consists of a
sequence of characters. Termination of each line in a text file is denoted with the end of line
(EOL). There are a few special characters that are used as EOL, but comma {,} and newlines
are the most common ones.
◉ Image files such as .jpg, .png, .gif, etc., and documents such as .doc, .xls, .pdf, etc., all of them
constitute binary files.
◉ Whatever is not text is a binary.
What is File Handling?
◉ The key function for working with files in Python is the open() function.
open()
Open File
Filename Mode
Work (Read or
Write or anything)
Syntax open(filename,mode)
Close File
Opening Files in Python
◉ The key function for working with files in Python is the open() function.
◉ Python has a built-in open() function to open a file. This function returns a file object, also called
a handle, as it is used to read or modify the file accordingly.
Syntax open(filename,mode)
filename: any name that you want mode: different modes for opening a file
“r” – Read – Default mode. Opens a file for reading, error if the file does not exist.
“a” – Append – Opens a file for appending, creates a file if does not exist.
“w” – Write – Opens a file for writing, creates the file if it does not exist.
“x” – Create – Creates the specified file, returns an error if the file exists.
Opening Files in Python (Cont.)
◉ We can specify the mode while opening a file. In mode, we specify whether we want to read r,
write w or append a to the file. We can also specify if we want to open the file in text mode
or binary mode.
“t” – Text– Default value. Text mode
“b” – Binary– Binary mode (e.g., images)
◉ The default is reading in text mode. In this mode, we get strings when reading from the file.
◉ On the other hand, binary mode returns bytes and this is the mode to be used when dealing with
non-text files like images or executable files.
Opening Files in Python (Cont.)
Mode Description
Opens a file for writing. Creates a new file if it does not exist or truncates
w
the file if it exists.
Opens a file for exclusive creation. If the file already exists, the operation
x
fails.
Opens a file for appending at the end of the file without truncating it.
a
Creates a new file if it does not exist.
file.read()
f = open(“test.txt”, “r”)
Example
print(f.read())
Reading Files in Python (Cont.)
◉ We can use the read(size) method to read in the size number of data. If the size parameter is not
specified, it reads and returns up to the end of the file.
◉ We can read the text.txt file we wrote in the above section in the following way:
Reading Files in Python (Cont.)
◉ We can see that the read() method returns a newline as '\n'. Once the end of the file is reached,
we get an empty string on further reading.
◉ We can change our current file cursor (position) using the seek() method. Similarly,
the tell() method returns our current position (in number of bytes).
Reading Files in Python (Cont.)
◉ Lastly, the readlines() method returns a list of remaining lines of the entire file. All these reading
methods return empty values when the end of file (EOF) is reached.
Reading Files in Python (Cont.)
f = open(“test.txt”, “r”)
Example
print(f.readline()) Line by line output
f = open(“test.txt”, “r”)
Example
print(f.readline(3)) Read three characters
.
f = open(“test.txt”, “r”)
Example
print(f.readlines()) Read lines separately
Looping over a File Object
◉ We can read a file line-by-line using a for loop. This is both efficient and fast.
◉ In this program, the lines in the file itself include a newline character \n. So, we use the end
parameter of the print() function to avoid two newlines when printing.
◉ Alternatively, we can use the readline() method to read individual lines of a file. This method
reads a file till the newline, including the newline character.
Writing to Files in Python
f = open(“test.txt”, “a”)
Example
f.write(“We love Python”)
Note: the “w”
method will
overwrite the entire
file.
f = open(“test.txt”, “w”)
Example
f.write(“We love Python”)
Writing to Files in Python (Cont.)
◉ In order to write into a file in Python, we need to open it in write w, append a or exclusive
creation x mode.
◉ We need to be careful with the w mode, as it will overwrite into the file if it already exists. Due to
this, all the previous data are erased.
◉ Writing a string or sequence of bytes (for binary files) is done using the write() method. This
method returns the number of characters written to the file.
◉ with open("test.txt",'w',encoding = 'utf-8') as f:
◉ f.write("my first file\n")
◉ f.write("This file\n\n")
◉ f.write("contains three lines\n")
◉ This program will create a new file named test.txt in the current directory if it does not exist. If it
does exist, it is overwritten.
◉ We must include the newline characters ourselves to distinguish the different lines.
Creating a new file
◉ To create a new file in Python, use the open() method with one of the following parameters:
“x” – Create – Creates the specified file, returns an error if the file exists.
“a” – Append – Opens a file for appending, creates a file if does not exist.
“w” – Write – Opens a file for writing, creates the file if it does not exist.
f = open(“test.txt”, “x”)
f = open(“test.txt”, “w”)
Deleting a file
◉ To delete a file, we must import the os module and run its os.remove() function
import os
Example
os.remove(“demo.txt”)
import os
if os.pathexists(“demo.txt”):
import os
os.remove(“demo.txt”)
os. rmdir(“foldername”)
else:
print(“File not exist”)
Closing Files in Python
◉ When we are done with performing operations on the file, we need to properly close the file.
◉ Closing a file will free up the resources that were tied with the file. It is done using
the close() method available in Python.
◉ Python has a garbage collector to clean up unreferenced objects but we must not rely on it to
close the file.
◉ f = open("test.txt", encoding = 'utf-8') # perform file operations
◉ f.close()
◉ This method is not entirely safe. If an exception occurs when we are performing some operation
with the file, the code exits without closing the file.
Closing Files in Python (Cont.)
◉ We can make certain mistakes while writing a program that lead to errors when we try to run it. A
python program terminates as soon as it encounters an unhandled error. These errors can be
broadly classified into two classes:
1. Syntax errors
2. Logical errors (Exceptions)
◉ Python Syntax Errors
◉ Error caused by not following the proper structure (syntax) of the language is called syntax
error or parsing error.
◉ Let's look at one example:
◉ As shown in the example, an arrow indicates where the parser ran into the syntax error.
◉ We can notice here that a colon : is missing in the if statement.
Python Exception Handling (Cont.)
Exceptions in Python
◉ 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.
◉ Python has many built-in exceptions that are raised when your program encounters an error (something in the
program goes wrong).
◉ When these exceptions occur, the Python interpreter stops the current process and passes it to the calling
process until it is handled. If not handled, the program will crash.
◉ For example, let us consider a program where we have a function A that calls function B, which in turn calls
function C. If an exception occurs in function C but is not handled in C, the exception passes to B and then to A.
◉ If never handled, an error message is displayed and our program comes to a sudden unexpected halt.
Python Exception Handling (Cont.)
◉ In this program, we loop through the values of the randomList list. As previously mentioned, the
portion that can cause an exception is placed inside the try block.
◉ If no exception occurs, the except block is skipped and normal flow continues(for last value). But
if any exception occurs, it is caught by the except block (first and second values).
◉ Here, we print the name of the exception using the exc_info() function inside sys module. We
can see that ’a’ causes ValueError and 0 causes ZeroDivisionError.
Python Exception Handling (Cont.)
◉ Example:
try:
print(x)
except NameError:
print("Variable x is not defined")
except:
print("Something else went wrong")
Python Exception Handling (Cont.)
EXAMPLE:
try:
#this will throw an exception if the file doesn't exist.
f = open("file.txt","r")
except IOError:
print("File not found")
else:
print("The file opened successfully")
f.close()
Python Exception Handling (Cont.)
Python Exception Handling (Cont.)
Python try...finally
◉ The try statement in Python can have an optional finally clause. This clause is executed no
matter what, and is generally used to release external resources.
◉ For example, we may be connected to a remote data center through the network or working with
a file or a Graphical User Interface (GUI).
◉ In all these circumstances, we must clean up the resource before the program comes to a halt
whether it successfully ran or not. These actions (closing a file, GUI or disconnecting from
network) are performed in the finally clause to guarantee the execution.
◉ This type of construct makes sure that the file is closed even if an exception occurs during the
program execution.
Python Exception Handling (Cont.)
EXAMPLE1:
try:
print(x)
except:
print("Something went wrong")
finally:
print("The 'try except' is finished")
EXAMPLE2:
try:
f = open("demofile.txt")
f.write(“HELLO")
except:
print("Something went wrong when writing to the file")
finally:
f.close()
Python Exception Handling (Cont.)
Python Exception Handling (Cont.)
Raise an exception
◉ As a Python developer you can choose to throw an exception if a condition occurs.
◉ In Python programming, exceptions are raised when errors occur at runtime. We can also
manually raise exceptions using the raise keyword.
◉ Example:
◉ Raise an error and stop the program if x is lower than 0:
◉ x = -1
if x < 0:
raise Exception("Sorry, no numbers below zero")
◉ The raise keyword is used to raise an exception.
◉ You can define what kind of error to raise, and the text to print to the user.
Python Exception Handling (Cont.)
◉ Example
Python Exception Handling (Cont.)
Python Directory
◉ If there are a large number of files to handle in our Python program, we can arrange our code
within different directories to make things more manageable.
◉ A directory or folder is a collection of files and subdirectories. Python has the os module that
provides us with many useful methods to work with directories (and files as well).
◉ Get Current Directory
◉ We can get the present working directory using the getcwd() method of the os module.
◉ This method returns the current working directory in the form of a string. We can also use
the getcwdb() method to get it as bytes object.
◉ Changing Directory
◉ We can change the current working directory by using the chdir() method.
◉ The new path that we want to change into must be supplied as a string to this method. We can
use both the forward-slash / or the backward-slash \ to separate the path elements.
Python Directory (Cont.)
Method Description
close() Closes an opened file. It has no effect if the file is already closed.
read(n) Reads at most n characters from the file. Reads till end of file if it is negative or None.
writable() Returns True if the file stream can be written to.
write(s) Writes the string s to the file and returns the number of characters written.
writelines(lines) Writes a list of lines to the file.
seek(offset,from=SEEK_SET) Changes the file position to offset bytes, in reference to from (start, current, end).
seekable() Returns True if the file stream supports random access.
tell() Returns the current file location.
truncate(size=None) Resizes the file stream to size bytes. If size is not specified, resizes to current location.
File Methods(Cont.)
Method Description
readline(n=-1) Reads and returns one line from the file. Reads in at most n bytes if specified.
readlines(n=-1) Reads and returns a list of lines from the file. Reads in at most n bytes/characters if specified.