IT Specialist Python - Session 6
IT Specialist Python - Session 6
• 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.
The block else allows you to run code when there are no errors.
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 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:
"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.
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
By default, the method read() returns the full text, but you can also
specify how many characters you want to return:
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: