bilisashobe430@gmail.
com
NSM9J7ZWYC
EXCEPTION AND FILE HANDLING
Proprietary
This filecontent. ©Greatfor
is meant Learning. All Rights
personal useReserved. Unauthorized use or distribution prohibited
by [email protected] only.
Sharing or publishing the contents in part or full is liable for legal action.
Agenda
• Introduction
• Open, read and write modes
• Adding text, counting characters
• Readline functions
[email protected]
NSM9J7ZWYC
• Try and except
• Try with else clause
• Finally keyword
DO NOT WRITE ANYTHING
HERE. LEAVE THIS SPACE FOR
WEBCAM
Proprietary
This filecontent. ©Greatfor
is meant Learning. All Rights
personal useReserved. Unauthorized use or distribution prohibited
by
[email protected] only.
Sharing or publishing the contents in part or full is liable for legal action.
[email protected]NSM9J7ZWYC
Introduction
Proprietary
This filecontent. ©Greatfor
is meant Learning. All Rights
personal useReserved. Unauthorized use or distribution prohibited
by [email protected] only.
Sharing or publishing the contents in part or full is liable for legal action.
Dealing with text files
• Dealing with text files is known as File Handling or even called as File I/O functions.
• There are different functions involved , which helps to read, write, append,
alter and perform many other functions.
[email protected]
NSM9J7ZWYC
• The IDE used is PyCharm and python version 3.9.1 or above
DO NOT WRITE ANYTHING
HERE. LEAVE THIS SPACE FOR
WEBCAM
Proprietary
This filecontent. ©Greatfor
is meant Learning. All Rights
personal useReserved. Unauthorized use or distribution prohibited
by
[email protected] only.
Sharing or publishing the contents in part or full is liable for legal action.
[email protected]NSM9J7ZWYC
Open, read and write modes
Proprietary
This filecontent. ©Greatfor
is meant Learning. All Rights
personal useReserved. Unauthorized use or distribution prohibited
by [email protected] only.
Sharing or publishing the contents in part or full is liable for legal action.
Open, read and write modes
Open mode
This mode is used whenever you want to open your text file for reading , writing
or doing some other stuffs.
Read mode
[email protected]
NSM9J7ZWYC
This mode is used whenever you want to read the text which is already stored in
Your text file
Write mode
This mode is used whenever you want to write the text in your .txt file
DO NOT WRITE ANYTHING
HERE. LEAVE THIS SPACE FOR
WEBCAM
Proprietary
This filecontent. ©Greatfor
is meant Learning. All Rights
personal useReserved. Unauthorized use or distribution prohibited
by
[email protected] only.
Sharing or publishing the contents in part or full is liable for legal action.
[email protected]NSM9J7ZWYC
Adding text, counting characters
Proprietary
This filecontent. ©Greatfor
is meant Learning. All Rights
personal useReserved. Unauthorized use or distribution prohibited
by [email protected] only.
Sharing or publishing the contents in part or full is liable for legal action.
Adding text, counting characters
Append function
• Append function helps us to add the text in your .txt file.
• The mode used is ‘a’ for appending means adding or writing some text to the file.
• For adding text to the file in a new line use \n before writing the sentence to be added.
[email protected]NSM9J7ZWYC
Len function
• To count the characters in a text file len() is the function used.
• Firstly we open the file and read that using the functions .
• And after the reading of the text len function is assigned with the variable
to find the count of characters
DO NOT WRITE ANYTHING
HERE. LEAVE THIS SPACE FOR
WEBCAM
Proprietary
This filecontent. ©Greatfor
is meant Learning. All Rights
personal useReserved. Unauthorized use or distribution prohibited
by
[email protected] only.
Sharing or publishing the contents in part or full is liable for legal action.
[email protected]NSM9J7ZWYC
Readline functions
Proprietary
This filecontent. ©Greatfor
is meant Learning. All Rights
personal useReserved. Unauthorized use or distribution prohibited
by [email protected] only.
Sharing or publishing the contents in part or full is liable for legal action.
Readline function
• This readline function helps us to read the text line by line.
• For using this function , firstly open file is needed and in whatever way you
want you can open the file, either read or write.
[email protected] Then accordingly use the readline function to read the lines accordingly in the
NSM9J7ZWYC
•
form of line by line.
DO NOT WRITE ANYTHING
HERE. LEAVE THIS SPACE FOR
WEBCAM
Proprietary
This filecontent. ©Greatfor
is meant Learning. All Rights
personal useReserved. Unauthorized use or distribution prohibited
by
[email protected] only.
Sharing or publishing the contents in part or full is liable for legal action.
[email protected]NSM9J7ZWYC
Try and Except
Proprietary
This filecontent. ©Greatfor
is meant Learning. All Rights
personal useReserved. Unauthorized use or distribution prohibited
by [email protected] only.
Sharing or publishing the contents in part or full is liable for legal action.
Try and Except Statement
● The code written inside the try block executes if the code is error free.
● If the code is not error free then the except block gets executed and results an exception.
● A try statement can have more than one except statement.
Syntax :
[email protected]
NSM9J7ZWYC
try:
Statement
except :
Exception
DO NOT WRITE ANYTHING
HERE. LEAVE THIS SPACE FOR
WEBCAM
Proprietary
This filecontent. ©Greatfor
is meant Learning. All Rights
personal useReserved. Unauthorized use or distribution prohibited
by [email protected] only.
Sharing or publishing the contents in part or full is liable for legal action.
[email protected]
NSM9J7ZWYC
Try with else clause
Proprietary
This filecontent. ©Greatfor
is meant Learning. All Rights
personal useReserved. Unauthorized use or distribution prohibited
by [email protected] only.
Sharing or publishing the contents in part or full is liable for legal action.
Try with else clause
● Else clause is used with the try clause when you want to execute the set of instructions in the
absence of exceptions in your code.
Syntax :
[email protected]
try:
NSM9J7ZWYC
print(“ “)
except :
print(“ “)
else:
print(“ “)
DO NOT WRITE ANYTHING
HERE. LEAVE THIS SPACE FOR
WEBCAM
Proprietary
This filecontent. ©Greatfor
is meant Learning. All Rights
personal useReserved. Unauthorized use or distribution prohibited
by
[email protected] only.
Sharing or publishing the contents in part or full is liable for legal action.
[email protected]NSM9J7ZWYC
Finally Keyword
Proprietary
This filecontent. ©Greatfor
is meant Learning. All Rights
personal useReserved. Unauthorized use or distribution prohibited
by [email protected] only.
Sharing or publishing the contents in part or full is liable for legal action.
Finally Keyword
● Finally is a keyword which surely executes after the execution of the try and except block of
statement.
try:
[email protected] print(“ “)
except:
NSM9J7ZWYC
print(“ “)
else : # Exceptional
print(“ “)
finally:
print(“ “)
DO NOT WRITE ANYTHING
HERE. LEAVE THIS SPACE FOR
WEBCAM
Proprietary
This filecontent. ©Greatfor
is meant Learning. All Rights
personal useReserved. Unauthorized use or distribution prohibited
by
[email protected] only.
Sharing or publishing the contents in part or full is liable for legal action.
Summary
Let’s take a quick recap
[email protected]
NSM9J7ZWYC
DO NOT WRITE ANYTHING
HERE. LEAVE THIS SPACE FOR
WEBCAM
Proprietary
This filecontent. ©Greatfor
is meant Learning. All Rights
personal useReserved. Unauthorized use or distribution prohibited
by
[email protected] only.
Sharing or publishing the contents in part or full is liable for legal action.
[email protected]NSM9J7ZWYC
Thank You
Proprietary
This filecontent. ©Greatfor
is meant Learning. All Rights
personal useReserved. Unauthorized use or distribution prohibited
by [email protected] only.
Proprietary content.
Sharing or©Great Learning.
publishing theAll Rights Reserved.
contents in part Unauthorized usefor
or full is liable or distribution
legal action.prohibited