Files and exception: text files, reading and writing files, format operator; command line
arguments, errors and exceptions, handling exceptions,multiple except block, modules,
packages; illustrative programs: word count, copy file, Voter’s age validation, Marks
range validation.
1.1 FILES
File is a named location on disk to store related information. It is used to
permanently store data in a non-volatile memory (e.g. hard disk).
Since, random access memory (RAM) is volatile which loses its data when
computer is turned off, we use files for future use of the data.
When we want to read from or write to a file we need to open it first. When we
are done, it needs to be closed, so that resources that are tied with the file are freed.
Hence, in Python, a file operation takes place in the following order.
1. Open a file
2. Read or write (perform operation)
3. Close the file
1.1.1 Opening a file
Python has a built-in function open() 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.
>>> f = open(“test.txt”) # open file in current directory
>>> f = open(“C:/Python33/README.txt”) # specifying full path
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 also specify if we want to open
the file in text mode or binary mode.
1
, 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 image or exe files.
The open function gets two arguments
1. File Name Name of the file
2. Mode indicates that files is opened to write
1.1.2 Python File Modes
Mode Description
‘r’ Open a file for reading. (default)
‘x’ Open a file for exclusive creation. If the file already exists, the
operation fails.
‘a’ Open for appending at the end of the file without truncating it. Creates
a new file if it does not exist.
‘t’ Open in text mode. (default)
‘b’ Open in binary mode.
‘+’ Open a file for updating (reading and w
2
, f = open(“test.txt”) # equivalent to ‘r’ or ‘rt’
f = open(“test.txt”,’w’) # write in text mode
f = open(“img.bmp”,’r+b’) # read and write in binary mode
Hence, when working with files in text mode, it is highly recommended to specify
the encoding type.
f = open(“test.txt”,mode = ‘r’)
1.1.3 Closing a File
When we are done with operations to the file, we need to properly close it.
Closing a file will free up the resources that were tied with the file and is done
using the close() method.
f= open(“test.txt”,’r’) # perform file operations
f.close()
1.1.4 Reading and writing
Reading from a file
A txt file is generally stored in the secondary media like hard disk drive, CD.
We know how to open and close a file object. But what are the actual commands
for reading? There are three ways to read from a file.
● read([n])
● readline([n])
● readlines()
Note: that n is the number of bytes to be read.
3