SlideShare a Scribd company logo
Introduction to 
Programming with 
Python – Class 5 
SYED FARJAD ZIA ZAIDI
Class Objectives 
Class Objective 
Understanding Python Modules, File I/O & Exceptions
Class Material 
•Chapter 7 - Python 
for Informatics: 
Exploring 
Information 
Reading
Python Modules 
 Definition: 
A module is a file containing Python definitions and 
statements. The file name is the module name with the 
suffix .py appended.
Import Statement 
 You can use any Python source file as a module by executing an import 
statement in some other Python source file. The import has the following 
syntax: 
import module1[, module2[,... moduleN]
Python Files I/O 
Until now, you have been reading and writing to the standard input and 
output. Now, we will see how to play with actual data files. 
Python provides basic functions and methods necessary to manipulate files 
by default. You can do your most of the file manipulation using a file object.
The open() Function 
Before you can read or write a file, you have to open it using Python's built-in 
open() function. This function creates a file object, which would be utilized 
to call other support methods associated with it. 
 Syntax: 
file object = open(file_name [, access_mode][, buffering])
File attributes 
Attribute Description 
file.closed Returns true if file is closed, false 
otherwise. 
file.mode Returns access mode with which file was 
opened. 
file.name Returns name of the file. 
file.softspace Returns false if space explicitly required 
with print, true otherwise.
The close() Method: 
 The close() method closes the file, and then no more operations on that file 
object can be performed.
The read() & write() method 
read() write() 
The read() method reads a string from an 
open file. 
The write() method writes any string to an 
open file. 
fileObject.read([count]); fileObject.write(string);
Exceptions 
 In general, when a Python script encounters a situation that it can't cope 
with, it raises an exception. An exception is a Python object that represents 
an error. 
 When a Python script raises an exception, it must either handle the 
exception immediately otherwise it would terminate and come out.
Handling exception 
 If you have some suspicious code that may raise an exception, you can 
defend your program by placing the suspicious code in a try: block. After 
the try: block, include an except: statement, followed by a block of code 
which handles the problem as elegantly as possible.
Syntax for handling exceptions 
try: 
You do your operations here; 
...................... 
except ExceptionI: 
If there is ExceptionI, then execute this block. 
except ExceptionII: 
If there is ExceptionII, then execute this block. 
...................... 
else: 
If there is no exception then execute this block.
Assignment 
 Link to Facebook Group: 
https://www.facebook.com/groups/introtopython.iccbs/

More Related Content

PPTX
Python Session - 2
PPTX
Python Session - 5
PPTX
Python Session - 6
PPTX
Python Session - 3
PPT
Javaio
PPTX
Intro to python
PDF
Python45 2
PDF
Java IO
Python Session - 2
Python Session - 5
Python Session - 6
Python Session - 3
Javaio
Intro to python
Python45 2
Java IO

What's hot (19)

PPTX
Datastrucure
PDF
Java I/o streams
PPTX
PDF
What is java input and output stream?
PPTX
Python-classes-in-mumbai
PPTX
Handling I/O in Java
PPS
Files & IO in Java
PPTX
Python 3 Programming Language
PPT
Basic input-output-v.1.1
PPTX
Introduction to Python for Data Science and Machine Learning
PDF
Bt0067 c programming and data structures2
PPTX
Introduction to python
DOCX
Filehandling
PPT
data file handling
PPTX
Introduction to python
ODP
Python Presentation
ODP
Moving to Python 3
PDF
Data file handling
PPTX
Python basics
Datastrucure
Java I/o streams
What is java input and output stream?
Python-classes-in-mumbai
Handling I/O in Java
Files & IO in Java
Python 3 Programming Language
Basic input-output-v.1.1
Introduction to Python for Data Science and Machine Learning
Bt0067 c programming and data structures2
Introduction to python
Filehandling
data file handling
Introduction to python
Python Presentation
Moving to Python 3
Data file handling
Python basics
Ad

Viewers also liked (20)

PDF
Introduction to Databases
PDF
Logic: Language and Information 1
PDF
An Introduction to Interactive Programming in Python 2013
PDF
Emerging Trends & Technologies in the Virtual K-12 Classroom
PDF
Internet History, Technology, and Security
PDF
Introduction to Computing with Java
PDF
Programming Mobile Applications for Android Handheld Systems 2014
PDF
Foundations of Virtual Instruction
PPTX
Vision & sight
PPT
Substituting HDF5 tools with Python/H5py scripts
PDF
Logic Over Language
PPTX
Introduction To Programming with Python-1
PDF
Python and HDF5: Overview
PPTX
Introduction To Programming with Python-4
PDF
Introduction to UBI
PPT
Python 4 Arc
PPT
The Python Programming Language and HDF5: H5Py
PPTX
Clase 2 estatica
PPT
Using HDF5 and Python: The H5py module
Introduction to Databases
Logic: Language and Information 1
An Introduction to Interactive Programming in Python 2013
Emerging Trends & Technologies in the Virtual K-12 Classroom
Internet History, Technology, and Security
Introduction to Computing with Java
Programming Mobile Applications for Android Handheld Systems 2014
Foundations of Virtual Instruction
Vision & sight
Substituting HDF5 tools with Python/H5py scripts
Logic Over Language
Introduction To Programming with Python-1
Python and HDF5: Overview
Introduction To Programming with Python-4
Introduction to UBI
Python 4 Arc
The Python Programming Language and HDF5: H5Py
Clase 2 estatica
Using HDF5 and Python: The H5py module
Ad

Similar to Introduction To Programming with Python-5 (20)

PPTX
Python files creation read,write Unit 5.pptx
PPTX
UNIT –5.pptxpython for engineering students
PPTX
Files and file objects (in Python)
PPT
Python File functions
PPTX
Programming in python - Week 7,8
PPTX
Unit-4 PPTs.pptx
PDF
GE3151_PSPP_UNIT_5_Notes
PPTX
Chapter - 5.pptx
PPTX
Unit V.pptx
PPTX
01 file handling for class use class pptx
PPT
File Handling as 08032021 (1).ppt
PPTX
pspp-rsk.pptx
PPTX
file handling in python using exception statement
PPTX
UNIT 5 PY.pptx - FILE HANDLING CONCEPTS
PDF
23CS101T PSPP python program - UNIT 5.pdf
PDF
File handling with python class 12th .pdf
PPTX
File handling in Python
PPTX
5-filehandling-2004054567151830 (1).pptx
PDF
Python files creation read,write Unit 5.pptx
UNIT –5.pptxpython for engineering students
Files and file objects (in Python)
Python File functions
Programming in python - Week 7,8
Unit-4 PPTs.pptx
GE3151_PSPP_UNIT_5_Notes
Chapter - 5.pptx
Unit V.pptx
01 file handling for class use class pptx
File Handling as 08032021 (1).ppt
pspp-rsk.pptx
file handling in python using exception statement
UNIT 5 PY.pptx - FILE HANDLING CONCEPTS
23CS101T PSPP python program - UNIT 5.pdf
File handling with python class 12th .pdf
File handling in Python
5-filehandling-2004054567151830 (1).pptx

More from Syed Farjad Zia Zaidi (19)

PDF
Web Application Architectures
PDF
Programming for Everybody (Python)
PDF
Learn to Program: The Fundamentals
PDF
Introduction to Systematic Program Design - Part 1
PDF
An Introduction to Interactive Programming in Python 2014
PDF
Human-Computer Interaction
PDF
Beginning Game Programming with C#
PDF
Computer Science 101
DOCX
Software Requirement Specification - Software Pack Solution 14
DOC
Project Proposal - Software Pack Solution 14
PDF
Database Diagram Tutorial-SQL Server 2012
PPTX
Introduction To Programming with Python-3
PPTX
Introduction To Programming with Python Lecture 2
DOCX
MindMuscle Xtreme
PDF
How to connect database file to a 3-Tier Architecture Application and obtain ...
PDF
PDF
Context Level DFD
PDF
PPTX
Virtual education, and Future of Virtual education in Pakistan.
Web Application Architectures
Programming for Everybody (Python)
Learn to Program: The Fundamentals
Introduction to Systematic Program Design - Part 1
An Introduction to Interactive Programming in Python 2014
Human-Computer Interaction
Beginning Game Programming with C#
Computer Science 101
Software Requirement Specification - Software Pack Solution 14
Project Proposal - Software Pack Solution 14
Database Diagram Tutorial-SQL Server 2012
Introduction To Programming with Python-3
Introduction To Programming with Python Lecture 2
MindMuscle Xtreme
How to connect database file to a 3-Tier Architecture Application and obtain ...
Context Level DFD
Virtual education, and Future of Virtual education in Pakistan.

Recently uploaded (20)

PPTX
Dynamic Solutions Project Pitch Presentation
PPTX
Save Business Costs with CRM Software for Insurance Agents
PDF
Community & News Update Q2 Meet Up 2025
PPTX
How a Careem Clone App Allows You to Compete with Large Mobility Brands
PPTX
Maximizing Revenue with Marketo Measure: A Deep Dive into Multi-Touch Attribu...
PPTX
Computer Hardware tool: hand tools, diagnostics, ESD and cleaning tools
PDF
Best Practices for Rolling Out Competency Management Software.pdf
PPTX
Presentation of Computer CLASS 2 .pptx
PDF
Jenkins: An open-source automation server powering CI/CD Automation
DOCX
The Future of Smart Factories Why Embedded Analytics Leads the Way
PDF
Teaching Reproducibility and Embracing Variability: From Floating-Point Exper...
PDF
Sensix-Tech-Pvt-Ltd-Company-Profile (1).pdf
PDF
The Future of Smart Factories Why Embedded Analytics Leads the Way
PDF
The Role of Automation and AI in EHS Management for Data Centers.pdf
PDF
Multi-factor Authentication (MFA) requirement for Microsoft 365 Admin Center_...
PDF
Micromaid: A simple Mermaid-like chart generator for Pharo
PPTX
AIRLINE PRICE API | FLIGHT API COST |
PDF
Why Should Businesses Extract Cuisine Types Data from Multiple U.S. Food Apps...
PDF
A Practical Breakdown of Automation in Project Management
PPTX
Materi-Enum-and-Record-Data-Type (1).pptx
Dynamic Solutions Project Pitch Presentation
Save Business Costs with CRM Software for Insurance Agents
Community & News Update Q2 Meet Up 2025
How a Careem Clone App Allows You to Compete with Large Mobility Brands
Maximizing Revenue with Marketo Measure: A Deep Dive into Multi-Touch Attribu...
Computer Hardware tool: hand tools, diagnostics, ESD and cleaning tools
Best Practices for Rolling Out Competency Management Software.pdf
Presentation of Computer CLASS 2 .pptx
Jenkins: An open-source automation server powering CI/CD Automation
The Future of Smart Factories Why Embedded Analytics Leads the Way
Teaching Reproducibility and Embracing Variability: From Floating-Point Exper...
Sensix-Tech-Pvt-Ltd-Company-Profile (1).pdf
The Future of Smart Factories Why Embedded Analytics Leads the Way
The Role of Automation and AI in EHS Management for Data Centers.pdf
Multi-factor Authentication (MFA) requirement for Microsoft 365 Admin Center_...
Micromaid: A simple Mermaid-like chart generator for Pharo
AIRLINE PRICE API | FLIGHT API COST |
Why Should Businesses Extract Cuisine Types Data from Multiple U.S. Food Apps...
A Practical Breakdown of Automation in Project Management
Materi-Enum-and-Record-Data-Type (1).pptx

Introduction To Programming with Python-5

  • 1. Introduction to Programming with Python – Class 5 SYED FARJAD ZIA ZAIDI
  • 2. Class Objectives Class Objective Understanding Python Modules, File I/O & Exceptions
  • 3. Class Material •Chapter 7 - Python for Informatics: Exploring Information Reading
  • 4. Python Modules  Definition: A module is a file containing Python definitions and statements. The file name is the module name with the suffix .py appended.
  • 5. Import Statement  You can use any Python source file as a module by executing an import statement in some other Python source file. The import has the following syntax: import module1[, module2[,... moduleN]
  • 6. Python Files I/O Until now, you have been reading and writing to the standard input and output. Now, we will see how to play with actual data files. Python provides basic functions and methods necessary to manipulate files by default. You can do your most of the file manipulation using a file object.
  • 7. The open() Function Before you can read or write a file, you have to open it using Python's built-in open() function. This function creates a file object, which would be utilized to call other support methods associated with it.  Syntax: file object = open(file_name [, access_mode][, buffering])
  • 8. File attributes Attribute Description file.closed Returns true if file is closed, false otherwise. file.mode Returns access mode with which file was opened. file.name Returns name of the file. file.softspace Returns false if space explicitly required with print, true otherwise.
  • 9. The close() Method:  The close() method closes the file, and then no more operations on that file object can be performed.
  • 10. The read() & write() method read() write() The read() method reads a string from an open file. The write() method writes any string to an open file. fileObject.read([count]); fileObject.write(string);
  • 11. Exceptions  In general, when a Python script encounters a situation that it can't cope with, it raises an exception. An exception is a Python object that represents an error.  When a Python script raises an exception, it must either handle the exception immediately otherwise it would terminate and come out.
  • 12. Handling exception  If you have some suspicious code that may raise an exception, you can defend your program by placing the suspicious code in a try: block. After the try: block, include an except: statement, followed by a block of code which handles the problem as elegantly as possible.
  • 13. Syntax for handling exceptions try: You do your operations here; ...................... except ExceptionI: If there is ExceptionI, then execute this block. except ExceptionII: If there is ExceptionII, then execute this block. ...................... else: If there is no exception then execute this block.
  • 14. Assignment  Link to Facebook Group: https://www.facebook.com/groups/introtopython.iccbs/