UNIT-3
Chapter 1 - File Handling
In everyday life, the term “file” is something of a catchall. It is used for things that are not only written, but
also used to describe things that don’t have any words in them at all, like pictures. A file is the common
storage unit in a computer, and all program and data are “written” into a file and “read” from a file. A file
extension, sometimes called a file suffix or a filename extension, is the character or group of characters
after the period that makes up an entire file name. The file extension helps your computer’s operating
system, like Windows, determine which program will be associated with the file.
For example, the file [Link] ends in docx, a file extension that is associated with Microsoft
Word on your computer. When you attempt to open this file, Windows sees that the file ending in a docx
extension and knows it should be opened with the Microsoft Word program. File extensions also often
indicate the file type, or file format, of the file but not always. Any file’s extensions can be renamed, but
that will not convert the file to another format or change anything about the file other than this portion of
its name.
Types of Files
Python supports two types of files – text files and binary files. These two file types may
look the same on the surface but they encode data differently.
1. Binary Files
While both binary and text files contain data stored as a series of bits (binary values of 1s and 0s), the bits
in text files represent characters, while the bits in binary files represent custom data. Binary files typically
contain a sequence of bytes or ordered groupings of eight bits. When creating a custom file format for a
program, a developer arranges these bytes into a format that stores the necessary information for the
application. Binary file formats may include multiple types of data in the same file, such as image, video,
and audio data. This data can be interpreted by supporting programs but will show up as garbled text in a
text editor. Below is an example of a .JPG image file opened in an image viewer and a text editor.
As you can see, the image viewer recognizes the binary data and displays the picture. When the image is
opened in a text editor, the binary data is converted to unrecognizable text. However, you may notice that
some of the text is readable. This is because the JPG format includes small sections for storing textual data.
2. Text files
Text files are more restrictive than binary files since they can only contain textual data. However, unlike
binary files, they are less likely to become corrupted. While a small error in a binary file may make it
unreadable, a small error in a text file may simply show up once the file has been opened. A typical plain
text file contains several lines of text that are each followed by an End-of-Line (EOL) character. An
End-of-File (EOF) marker is placed after the final character, which signals the end of the file. Text files
include a character encoding scheme that determines how the characters are interpreted and what characters
1
can be displayed. Since text files use a simple, standard format, many programs are capable of reading and
editing text files. Common text editors include Microsoft Notepad and WordPad, which are bundled with
Windows, and Apple TextEdit, which is included with Mac OS X.
We can usually tell if a file is binary or text based on its file extension. This is because by convention the
extension reflects the file format, and it is ultimately the file format that dictates whether the file data is
binary or text.
Common extensions for binary file formats:
Images: jpg, png, gif, bmp, tiff, psd,...
Videos: mp4, mkv, avi, mov, mpg, vob,...
Audio: mp3, aac, wav, flac, ogg, mka, wma,...
Documents: pdf, doc, xls, ppt, docx, odt,...
Archive: zip, rar, 7z, tar, iso,...
Database: mdb, accde, frm, sqlite,...
Executable: exe, dll, so, class,...
Common extensions for text file formats:
Web standards: html, xml, css, svg, json,...
Source code: c, cpp, h, cs, js, py, java, rb, pl, php, sh,...
Documents: txt, tex, markdown, asciidoc, rtf, ps,...
Configuration: ini, cfg, rc, reg,...
Tabular data: csv, tsv,...
Difference between Text files and Binary files
Text File Binary File
Contains data that is human-readable and Contain data in a format that is not human-readable.
structured as a series of characters.
Used for documents, source code, Used for executable programs, images, videos,
configuration files, and logs. audio, compressed files, and databases.
More portable across different platforms, May not be directly portable due to differences in
since they rely on widely standardized how different systems represent binary data.
character encoding systems.
Use platform-specific end-of-line (EOL) Do not have special end-of-line markers, as the data
characters (e.g., \n on Unix/Linux, \r\n on is not structured around human-readable lines.
Windows).
Can be opened and edited using simple text Typically require specialized software to open and
editors like Notepad or vim. manipulate, such as a hex editor or a program
designed to process that specific binary format.
Small errors or corruption might only affect Even minor corruption can render the entire file
certain lines or characters. unusable since the structure is more rigid.
File Names and Paths
All operating systems follow the same general naming conventions for an individual file: a base file name
and an optional extension, separated by a period. Note that a directory I simply a file with a special
attribute designating it as a directory, but otherwise must follow all the same naming rules as a regular file.
To make use of files, you have to provide a file path, which is basically a route so that the user or the
program knows where the file is located. The following fundamental rules enable applications to create
and process valid names for files and directories in both Windows and Linux operating systems unless
explicitly specified:
2
Use a period to separate the base file name from the extension in the file name.
In Windows use backslash (\) and in Linux use forward slash (/) to separate the components of a path.
The backslash (or forward slash) separates one directory name from another directory name in a path
and it also divides the file name from the path leading to it. Backslash (\) and forward slash (/) are
reserved characters and you cannot use them in the name for the actual file or directory.
Do not assume case sensitivity. File and Directory names in Windows are not case sensitive while in
Linux it is case sensitive. For example, the directory names ORANGE, Orange, and orange are the
same in Windows but are different in Linux Operating System.
In Windows, volume designators (drive letters) are case-insensitive. For example, "D:\" and "d:\" refer
to the same drive.
The reserved characters that should not be used in naming files and directories are < (less than), >
(greater than),: (colon), " (double quote), / (forward slash), \ (backslash), | (vertical bar or pipe), ?
(question mark) and * (asterisk).
In Windows Operating system reserved words like CON, PRN, AUX, NUL, COM1, COM2, COM3,
COM4, COM5, COM6, COM7, COM8, COM9, LPT1, LPT2, LPT3, LPT4, LPT5, LPT6, LPT7, LPT8,
and LPT9 should not be used to name files and directories.
Fully Qualified Path and Relative Path
A file path can be a fully qualified path name or relative path.
Fully Qualified path
The fully qualified path name is also called an Absolute path. A path is said to be a fully qualified path if
it points to the file location, which always contains the root and the complete directory list. The current
directory is the directory in which a user is working at a given time. Every user is always working within
a directory. will be C:\ and the root directory on your Linux system will be / (forward slash).
Examples of a fully qualified path are given below.
"C:\[Link]" refers to a file named "[Link]" under root directory C:\.
"C:\fauna\[Link]" refers to a file named "[Link]" in a subdirectory fauna under root directory C:\.
Relative path
A path is also said to be a relative path if it contains “double-dots”; that is, two consecutive periods together
as one of the directory components in a path or “single-dot”; that is, one period as one of the directory
components in a path. These two consecutive periods are used to denote the directory above the current
directory, otherwise known as the “parent directory.” Use a single period as a directory component in a
path to represent the current directory.
Examples of the relative path are given below:
"..\[Link]" specifies a file named "[Link]" located in the parent of the current directory fauna.
".\[Link]" specifies a file named "[Link]" located in a current directory named fauna.
"..\..\[Link]" specifies a file that is two directories above the current directory india.
The following figure shows the structure of sample directories and files.
Figure: Structure of files and directories
3
Difference between fully qualified path and relative path
Fully Qualified Path Relative Path
A fully qualified path provides the complete path A relative path is defined in relation to the
from the root directory to the file or folder, starting current working directory or another base
from the top-most directory in the file system directory. It specifies the location of a file or
hierarchy. folder relative to the current location in the file
system.
On Windows: Typically starts with a drive letter Does not start with a root directory or drive
(e.g., C:\). letter; instead, it can start directly with the
On Linux/Mac: Starts with the root directory /. name of a directory or file, or use special
symbols like: . (current directory) .. (parent
directory)
Absolute paths are generally not portable across Relative paths are more portable across
different systems. different environments.
Example: Example:
Windows:C:\Users\John\Documents\[Link] "..\[Link]", ".\[Link]", "..\..\[Link]"
Linux/Mac: /home/john/documents/[Link]
Creating and Opening Text Files
Files are not very useful unless you can access the information they contain. All files must be opened first
before they can be read from or written to using the Python’s built-in open() function. When a file is opened
using open() function, it returns a file object called a file handler that provides methods for accessing the
file.
The syntax of open() function is given below.
file_handler = open(filename, mode)
The open() function returns a file handler object for the file name.
The open() function is commonly used with two arguments, where the first argument is a string
containing the file name to be opened which can be absolute or relative to the current working directory.
The second argument is another string containing a few characters describing the way in which the file
will be used as shown in table below.
The mode argument is optional; r will be used if it is omitted.
The file handler itself will not contain any data pertaining to the file.
Different Modes of Opening File
Mode Description
“r” Opens the file in read only mode and this is the default mode.
“w” Opens the file for writing. If a file already exists, then it’ll get overwritten. If the file does
not exist, then it creates a new file.
“a” Opens the file for appending data at the end of the file automatically. If the file does not
exist it creates a new file
“r+” Opens the file for both reading and writing.
“w+” Opens the file for reading and writing. If the file does not exist it creates a new file. If a
file already exists then it will get overwritten.
“a+” Opens the file for reading and appending. If a file already exists, the data is appended. If
the file does not exist it creates a new file.
4
“x” Creates a new file. If the file already exists, the operation fails.
“rb” Opens the binary file in read-only mode.
“wb” Opens the file for writing the data in binary format.
“rb+” Opens the file for both reading and writing in binary format.
For example,
1. >>> file_handler = open("[Link]","x")
2. >>> file_handler = open("[Link]","r")
3. >>> file_handler = open("C:\[Link]","r")
4. >>> file_handler = open("C:\prog\[Link]","r")
5. >>> file_handler = open("C:\\fauna\\[Link]","r")
6. >>> file_handler = open("C:\\network\[Link]","r")
7. >>> file_handler = open(r"C:\network\[Link]","r")
8. >>> file_handler = open("[Link]","r")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
FileNotFoundError: [Errno 2] No such file or directory: '[Link]'
9. >>> file_handler = open("[Link]","w+")
10. >>> file_handler = open("[Link]","a+")
When the Python interpreter reads the path, the characters \f and \b in the path will be treated as escape
sequences and not as part of directory or text name. Python represents backslashes in strings as \\ because
the backslash is an escape character—for instance, \n represents a new line, \t represents a tab, \f represents
ASCII Formfeed, and \b represents ASCII Backspace. Because of this, there needs to be a way to tell Python
you really want two characters of \f and \b rather than Formfeed and Backspace, and you do that by escaping
the backslash itself with another one.
File close() Method
Opening files consume system resources, and, depending on the file mode, other programs may not be able
to access them. It is important to close the file once the processing is completed. After the file handler
object is closed, you cannot further read or write from the file. Any attempt to use the file handler object
after being closed will result in an error.
The syntax for close() function is,
file_handler.close()
For example,
1. >>> file_handler = open("[Link]","r")
2. >>> file_handler.close()
You should call file_handler.close() to close the file. This immediately frees up any system resources used
by it ➁. If you do not explicitly close a file, Python’s garbage collector will eventually destroy the object
and close the opened file for you, but the file may have stayed open for a while. Another risk is that different
Python implementations will do this cleanup at different times.
If an exception occurs while performing some operation on the file, then the code exits
without closing the file. In order to overcome this problem, you should use a try-except finally block to
handle exceptions. For example,
try:
f = open("file", "w")
try:
[Link]('Hello World!')
5
finally:
[Link]()
except IOError:
print('oops!')
You should not be writing to the file in the finally block, as any exceptions raised
there will not be caught by the except block.
file_handler = open("[Link]")
print("Printing each line in the text file")
for each_line in file_handler:
print(each_line)
file_handler.close()
Use of with Statements to Open and Close Files
Instead of using try-except-finally blocks to handle file opening and closing operations, a much cleaner
way of doing this in Python is using the with statement. You can use a with statement in Python such that
you do not have to close the file handler object.
Syntax:
with open (file, mode) as file_handler:
Statement_1
Statement_2
.
.
.
Statement_N
In the syntax, the words with and as are keywords and the with keyword is followed by the open()
function and ends with a colon.
The as keyword acts like an alias and is used to assign the returning object from the open() function to
a new variable file_handler.
The with statement creates a context manager and it will automatically close the file handler object for
you when you are done with it, even if an exception is raised on the way, and thus properly managing
the resources.
Example:
print("Printing each line in text file")
with open("[Link]") as file_handler:
for each_line in file_handler:
print(each_line, end="")
You can also use a with statement to open more than one file. For example,
with open(in_filename) as in_file, open(out_filename, 'w') as out_file:
for line in in_file:
.
.
.
out_file.write(parsed_line)
6
File Object Attributes
When the Python open() function is called, it returns a file object called a file handler. Using this file
handler, you can retrieve information about various file attributes.
Attribute Description
file_handler.closed It returns a Boolean True if the file is closed or False otherwise.
file_handler.mode It returns the access mode with which the file was opened.
file_handler.name It returns the name of the file.
For example,
1. >>> file_handler = open("[Link]", "w")
2. >>> print(f"File Name is {file_handler.name}")
File Name is [Link]
3. >>> print(f"File State is {file_handler.closed}")
File State is False
4. >>> print(f"File Opening Mode is {file_handler.mode}")
File Opening Mode is w
File Methods to Read and Write Data
When you use the open() function a file object is created. Here is the list of methods that can be called on
this object.
Method Syntax Description
read() file_handler This method is used to read the contents of a file up to a size
.read([size]) and return it as a string. The argument size is optional, and, if
it is not specified, then the entire contents of the file will be
read and returned.
readline() file_handler This method is used to read a single line in file.
.readline()
readlines() file_handler This method is used to read all the lines of a file as list items.
.readlines()
write() file_handler. This method will write the contents of the string to the file,
write(string) returning the number of characters written. If you want to
start a new line, you must include the new line character.
writelines() file_handler. This method will write a sequence of strings to the file.
writelines(sequence)
tell() file_handler.tell() This method returns an integer giving the file handler’s
current position within the file, measured in bytes from the
beginning of the file.
seek() file_handler. This method is used to change the file handler’s position. The
seek(offset, position is computed from adding offset to a reference point.
from_what) The reference point is selected by the from_what argument.
A from_what value of 0 measures from the beginning of the
file, 1 uses the current file position, and 2 uses the end of the
file as the reference point. If the from_what argument is
omitted, then a default value of 0 is used, indicating that the
beginning of the file itself is the reference point.
7
For example, ab
1. >>> f = open("[Link]", "w") 6. >>> print([Link](2))
2. >>> [Link]("abcdefgh") cd
8 7. >>> print([Link](2))
3. >>> [Link]() ef
4. >>> f = open("[Link]") 8. >>> print([Link](2))
5. >>> print([Link](2)) gh
In the above code, the size argument is specified in the read() method. In statement ➄, the first two
characters are read. In statement ➅, next two characters are read. This goes on until the end of the file
is reached depending on the size of the argument ➆–➇.
Example: Write Python Program to Read "[Link]" File Using read() Method. Sample Content
of "[Link]" File is Given Below.
Ancient Rome was a civilization which began on the Italian Peninsula in the 8th century BC.
The Roman Emperors were monarchial rulers of the Roman State.
The Emperor was supreme ruler of Rome.
Rome remained a republic.
Output
with open("[Link]") as file_handler: Print entire file contents
print("Print entire file contents") Ancient Rome was a civilization which began on the Italian
Peninsula in the 8th century BC.
print(file_handler.read(), end="") The Roman Emperors were monarchial rulers of the Roman State.
The Emperor was supreme ruler of Rome.
Rome remained a republic.
Consider the "[Link]" File Specified in
above program Write Python Program to Read
"[Link]" file Using readline() Method Output
with open("[Link]") as file_handler: Print a single line from the file
Ancient Rome was a civilization which began on
print("Print a single line from the file") the Italian Peninsula in the 8th century BC.
print(file_handler.readline(), end="") Print another single line from the file
The Roman Emperors were monarchial rulers of
print("Print another single line from the file") the Roman State.
print(file_handler.readline(), end="")
Consider the "[Link]" Write Python Program to Read "[Link]" File Using readlines()
Method
Output
with open("[Link]") as file_handler: ['Ancient Rome was a civilization which began
print("Print file contents as a list") on the Italian Peninsula in the 8th century
BC.\n', 'The Roman Emperors were monarchial
print(file_handler.readlines()) rulers of the Roman State.\n', 'The Emperor
was supreme ruler of Rome.\n', 'Rome remained
a republic.']
The following code demonstrates the write() method.
1. >>> file_handler = open("[Link]","w")
2. >>> file_handler.write("Moon is a natural satellite")
27
3. >>> file_handler.close()
4. >>> file_handler = open("[Link]", "a+")
5. >>> file_handler.write("of the earth")
12
8
6. >>> file_handler.close()
7. >>> file_handler = open("[Link]")
8. >>> file_handler.read()
'Moon is a natural satelliteof the earth'
9. >>> file_handler.close()
10. >>> file_handler = open("[Link]","w")
11. >>> file_handler.writelines(["Moon is a natural satellite", " ", "of the earth"])
12. >>> file_handler.close()
13. >>> file_handler = open("[Link]")
14. >>> file_handler.read()
'Moon is a natural satellite of the earth'
15. >>> file_handler.close()
The file [Link] is opened in write mode ➀. If the file is not present, then the file is created. The
write() method is used to write the string to the [Link] file using the file_handler object ➁. This
statement returns the number of characters written. Once the file_handler
print(file_handler.readlines())
The seek() method: Syntax- file_handler. seek(offset, from_what)
The seek() method is used to set/change the file handler’s current position. Never forget that when
managing files, there’ll always be a position inside that file where you are currently working on. When
you open a file, that position is the beginning of the file, but as you work with it, you may advance.
The seek() method will be useful to you when you need to work with that open file. For example,
1. >>> f = open('workfile', 'w') 5. >>> [Link](5)
2. >>> [Link]('0123456789abcdef') 5
16 6. >>> [Link]()
3. >>> [Link]() '56789abcdef'
4. >>> f = open('workfile', 'r')
The workfile file is opened in 'w' mode ➀ and some contents are written ➁ and the file handler is
closed ➂. The same file is again opened in 'r' mode ➃. The file handler starts reading from the 6th
character ➄, but counting starts from 0th character. It returns the character of the latest position from
where the file handler will start to read.
1. >>> f = open('workfile', 'w') 6. >>> [Link](2, 1)
2. >>> [Link]('0123456789abcdef') 4
16 7. >>> [Link]()
3. >>> [Link]() b'456789abcdef'
4. >>> f = open('workfile', 'rb+') 8. >>> [Link](-3, 2)
5. >>> [Link](2) 13
2 9. >>> [Link]()
b'def'
The tell() method: Syntax- file_handler.tell()
The tell() method returns the file handler’s current position. For example,
1. >>> f = open('workfile', 'w')
2. >>> [Link]('0123456789abcdef')
16
3. >>> [Link]()
9
SPC/CS/BCA/IV SEM/Python Programming
4. >>> f = open('workfile')
5. >>> s1 = [Link](2)
6. >>> print(s1)
01
7. >>> [Link]()
2
8. >>> s2 = [Link](3)
9. >>> print(s2)
234
10. >>> [Link]()
5
Write Python Program to Count the Occurrences of Each Word and Also Count the Number of
Words in a text File.
occurrence_of_words = dict()
total_words = 0
with open("[Link]") as file_handler:
for each_row in file_handler:
words = each_row.rstrip().split()
total_words += len(words)
for each_word in words:
occurrence_of_words[each_word] = occurrence_of_words.get(each_word, 0) +1
print("The number of times each word appears in a sentence is")
print(occurrence_of_words)
print(f"Total number of words in the file are {total_words}")
10