Python :
File Handling and Exception
Punit A. Lathiya
Assistant Professor
EC Department
GEC Rajkot
Syllabus : 3151108
Syllabus : 3151108
Teaching and Examination Scheme
Books
Online Courses
• [Link]
• [Link]
• [Link]
Index
• Introduction
• Read/Write
▫ Text Files
▫ CSV Files
▫ Binary Files
• Handling Exception
Introduction
• File handling is important for
▫ storing/writing data to file/memory
▫ Reading from file/memory
• Important for database management
• Syntax to access file in Python:
▫ F=Open(‘file_name.ext’, ‘mode’)
• F is file handler
• Mode is read/write/append
Introduction
• Types of File:
▫ Text Files (Readable by humans and computers)
▫ Binary Files (Readable by computer only)
Function Methods
[Link]() Read file character-wise
[Link]() Read file line wise
[Link]() Read multiple lines
[Link]() Show status of file open or closed True or false
[Link]() Show access mode of file r,w,a etc
[Link] Show file name
[Link]() Get the current file position
[Link](0) bring file cursor to initial position
[Link]() Close file
[Link]() Write or append in to file
Mode for opening file
Reading Text File
[Link]
[Link]
Writing to Text File
Output : [Link]
Reading/Writing Binary Files
• Image file (.jpg, .png, .tiff etc), .ini file, .exe file
• Contain Non-readable characters
• Use Mode as “rb”, “rb+” , “wb+”, “ab+”
• Rest of code structure will be as it is
CSV File:
• CSV : Comma Separated Value
• This is a text format intended for the presentation
of tabular data.
• Each line of the file is one line of the table.
• The values of individual columns are separated by
a separator symbol - a comma (,), a semicolon (;)
or another symbol.
CSV File:
Language Designed By Appeared Extension
• Table Data:
Python Guido van 1991 .py
Rossum
Java James Gosling 1995 .java
C++ Bjarne Stroustrup 1983 .cpp
• CSV File:
Programming language, Designed by, Appeared, Extension
Python, Guido van Rossum, 1991, .py
Java, James Gosling, 1995, .java
C++, Bjarne Stroustrup,1983,.cpp
Reading CSV File
Reading SCV File as Dictionary
Writing CSV File
Exception
• Compile time error
▫ Syntax error
Missing semicolons, colons
Wrong spelling/keywords
• Logical error
▫ Code is compiled, output is available but wrong
• Run time error
▫ Divide by zero, value error, file linking error
Exception
• Divide code statements in two parts
▫ Normal statement : Will not give any error
▫ Critical Statement : May give error
Normal Statement
Critical Statement
Example: Divide by zero
If no
error
If error
Example: Divide by zero
If no
error
If error
Example : Value Error
Example : Value Error
Example : File Linking Error
Problems:
1. File Doesn’t Exist for
reading
2. Resources(Files) are not
closed as of error
Example : File Linking Error
Problem: Problem:
1. If error (File Not Found) 1. If No error, File will not be
comes, File will not be closed. closed.
Example : File Linking Error
Solution :
Add Finally Block
Multiple Exception
Problem :
Error is different
then what it appears.
Multiple Exception
Multiple Exception
Assignment
• Explain need of file handling in C/Python
• Explain types of error.
• Enlist run-time error comes in python
• Explain try-except-finally block with example.
Assignment Program
1. Write Python code to print program itself
2. Read CSV file containing marks of student. Find
1. Student with Max marks
2. Total Student who fails (Marks<8)
3. Class Average
3. Write CSV file for first 100 fibonacci numbers
4. Read CSV file containing smart-phone specifications,
print only model number and price.
Thank You