Python Lessons – 3.8 The Python Standard Library and pip
All modules belonging to the last typology are part of a library called standard library. This library contains many modules, each specialized to fulfill certain operations.
Never in the same shape
All modules belonging to the last typology are part of a library called standard library. This library contains many modules, each specialized to fulfill certain operations.
These exceptions happen when something goes wrong in your code by generating an error. The reasons why these errors are generated can be many, for example due to a resource not found for example a file, or to a value with an incompatible format for a particular operation, etc.
When code execution encounters an error, it throws an exception that stops the program from running. To avoid this you can handle the exception, in order to correct the error, have the possibility to remedy or provide an output information on the nature of the error occurred so then eventually understand the nature and correct the code.
In Python There is an additional clause to add to the try-except construct called finally. This clause also defines a specific block of code that will be executed in all cases. That is, the finally block is executed both if the try block is terminated without exception, or if any exception occurred (both managed and unmanaged).
It can happen sometimes that we ourselves want to create and throw an exception. The reasons for doing this can be many. Often you might want to create a more specific exception, which signals a specific error to those who are using our code in the future.
Assertions (assert) are a very useful control tool to use when writing a code in the test (DEBUG) phase. Each assertion introduced in the code performs a check at a precise time of execution, using a condition. If the condition is satisfied then the execution goes forward, otherwise a Asserterror exception is raised that blocks the execution of the program.
After you open the file in read mode, you will need to read its contents. If the file is text-type, you can use the Read () method.
To write to a file, first you have to open a file in write mode, and then you use the Write () method specifying as an argument what we want to write
In the previous sections we have seen how to read and write the data within a file, but there are some precautions to follow. We have seen that it is important to always close the file we are working on while running the program.
The None object is used when you want to represent the absence of a value. It has very similar functions to NULL in other programming languages.