introduction to programming with python-5
DESCRIPTION
Slides for Lecture 5 of the course: Introduction to Programming with Python offered at ICCBS. It covers the following topics: 1.)Python Modules 2.)File I/O 3.)Exceptions & Error HandlingTRANSCRIPT
Introduction to Programming with Python – Class 5SYED 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/