introduction to programming with python-5

14
Introduction to Programming with Python – Class 5 SYED FARJAD ZIA ZAIDI

Upload: syed-farjad-zia-zaidi

Post on 26-May-2015

115 views

Category:

Software


4 download

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 Handling

TRANSCRIPT

Page 1: Introduction To Programming with Python-5

Introduction to Programming with Python – Class 5SYED FARJAD ZIA ZAIDI

Page 2: Introduction To Programming with Python-5

Class Objectives

Class Objective

Understanding Python Modules, File I/O & Exceptions

Page 3: Introduction To Programming with Python-5

Class Material

• Chapter 7 - Python for Informatics: Exploring Information

Reading

Page 4: Introduction To Programming with Python-5

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.

Page 5: Introduction To Programming with Python-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]

Page 6: Introduction To Programming with Python-5

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.

Page 7: Introduction To Programming with Python-5

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])

Page 8: Introduction To Programming with Python-5

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.

Page 9: Introduction To Programming with Python-5

The close() Method:

The close() method closes the file, and then no more operations on that file object can be performed.

Page 10: Introduction To Programming with Python-5

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);

Page 11: Introduction To Programming with Python-5

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.

Page 12: Introduction To Programming with Python-5

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.

Page 13: Introduction To Programming with Python-5

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.

Page 14: Introduction To Programming with Python-5

Assignment

Link to Facebook Group:

https://www.facebook.com/groups/introtopython.iccbs/