Written by students who passed Immediately available after payment Read online or as PDF Wrong document? Swap it for free 4.6 TrustPilot
logo-home
Summary

Summary Files and Exceptions in Python with programs

Rating
-
Sold
-
Pages
30
Uploaded on
03-07-2025
Written in
2024/2025

Python files and Exceptions provides a detailed explanation of key concepts in python like file handling , file modes and methods, format specifiers, built in exceptions, modules and packages and some other sample program

Institution
Course

Content preview

UNIT IV - FILES AND EXCEPTION
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

Written for

Institution
Course

Document information

Uploaded on
July 3, 2025
Number of pages
30
Written in
2024/2025
Type
SUMMARY

Subjects

$3.99
Get access to the full document:

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Get to know the seller
Seller avatar
mohanapriyasubramaniyan

Get to know the seller

Seller avatar
mohanapriyasubramaniyan s.a engineering college
Follow You need to be logged in order to follow users or courses
Sold
-
Member since
10 months
Number of followers
0
Documents
2
Last sold
-

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions