0% found this document useful (0 votes)
2 views

IT Specialist Python - Session 6

This document covers exception handling in Python using try, except, else, and finally blocks, explaining how to manage errors in code execution. It also details file management, including how to open, read, write, and delete files using various modes and functions. Additionally, it includes an exercise to create a Python program for storing and managing student information.

Uploaded by

Alejandra Campos
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

IT Specialist Python - Session 6

This document covers exception handling in Python using try, except, else, and finally blocks, explaining how to manage errors in code execution. It also details file management, including how to open, read, write, and delete files using various modes and functions. Additionally, it includes an exercise to create a Python program for storing and managing student information.

Uploaded by

Alejandra Campos
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

IT Specialist - Python

Session 6 – No-SQL Database Integration


What will we see now?

• Try Exception
• File management
Try except
exception handling
When an error, or exception as we call it, occurs, Python will
normally stop and output an error message.

These exceptions can be handled using the try statement:


Try except
The block try allows you to test a block of code for errors.

The block except allows you to handle the error.

The block else allows you to run code when there are no errors.

The block finally allows you to execute code, regardless of the


result of the test and exception blocks.
Try except
More than one exceptions

You can define as many exception blocks as you like, for example if
you want to execute a special code block for a special type of error:
Try except
else

You can use the keyword else to define a block of code that will be
executed if no errors are raised:
Tryexcept
Finally

The block finally, if specified, will be executed regardless of


whether the try block fails or not.
File management
File handling is an important part of any web application. Python
has several functions to create, read, update, and delete files.

The key function for working with files in Python is the open()
function.

The open() function takes two parameters; file name and mode.
File management
There are four different methods (modes) for opening a file:

"r"- Read - Default value.


Open a file for reading, error if the file does not exist

"to"- Add:
Open a file to add, create the file if it doesn't exist

"w"- Write:
Open a file for writing, create the file if it doesn't exist

"x"- Create:
Creates the specified file, returns an error if the file exists
File management
Mode Description Behavior

r Read-only mode. Opens the file for reading. File must exist; otherwise, it raises an error.

Opens the file for reading binary data. File must exist; otherwise, it raises an
rb Read-only in binary mode.
error.

Opens the file for both reading and writing. File must exist; otherwise, it raises
r+ Read and write mode.
an error.

Opens the file for both reading and writing binary data. File must exist;
rb+ Read and write in binary mode.
otherwise, it raises an error.

w Write mode. Opens the file for writing. Creates a new file or truncates the existing file.

Opens the file for writing binary data. Creates a new file or truncates the
wb Write in binary mode.
existing file.

Opens the file for both writing and reading. Creates a new file or truncates the
w+ Write and read mode.
existing file.

Opens the file for both writing and reading binary data. Creates a new file or
wb+ Write and read in binary mode.
truncates the existing file.

a Append mode. Opens the file for appending data. Creates a new file if it doesn’t exist.

ab Append in binary mode. Opens the file for appending binary data. Creates a new file if it doesn’t exist.

a+ Append and read mode. Opens the file for appending and reading. Creates a new file if it doesn’t exist.

Opens the file for appending and reading binary data. Creates a new file if it
ab+ Append and read in binary mode.
doesn’t exist.

x Exclusive creation mode. Creates a new file. Raises an error if the file already exists.

xb Exclusive creation in binary mode. Creates a new binary file. Raises an error if the file already exists.

x+ Exclusive creation with read and write mode. Creates a new file for reading and writing. Raises an error if the file exists.

Creates a new binary file for reading and writing. Raises an error if the file
xb+ Exclusive creation with read and write in binary mode.
exists.
File management
Additionally, you can specify whether the file should be handled as
binary or text mode.

"t"- Text - Default value. text mode

"b"- Binary - Binary mode (for example, images)


File management
Syntax

To open a file for reading, simply specify the file name:

The above code is the same as:

Because "r“ for read and "t“ for text are the default values, you do
not need to specify them.
File management
Open a file on the server

Suppose we have the following file, located in the same folder as


Python:
File management
To open the file, use the built-in function open().
The open() function returns a file object, which has a methodread()
to read the contents of the file:

If the file is in a different location, you'll need to specify the file


path, like so:
File management
Read only parts of the file

By default, the method read() returns the full text, but you can also
specify how many characters you want to return:

*Returns the first 5 characters of the file


File management
read lines
You can return a line using the method readline() :

By stepping through the lines of the file, you can read the entire
file, line by line:
File management
close files
It is good practice to always close the file when you are done with
it.

Note: You should always close your files; In some cases, due to
buffering, changes to a file may not be displayed until you close the
file.
File management
Write to an existing file
To write to an existing file, you must add a parameter tothe open()
function:

"a"- Add: will be added to the end of the file

"w"- Write: Will overwrite any existing content


File management
Open the file "demofile2.txt"
and add content to the file:
File management
delete a file
To remove a file, you need to import the OS module and run
its function os.remove() :
File management
Exercise

Create a Python code that allows you to store student


information and then review it. This should have the following
menu:

- Add content to file


- See file, in this option you must list the list
- Delete file
Doubts or Questions

You might also like