Python Exception handling, I/O Stream, Modules and packages

Share:

Exception handling

Handling the errors in the program without program crash is done by the exception handling, exception handling has three main keyword (try, except and finally) which will be covered below but now let’s look at the structure of the try block:

               try:
                                codes
                except:
                                codes
                finally:
                                codes


Try:
Codes after this keyword will be checked for errors.

except:
if there was an error in the try block except block is responsible to handle that. We can use an else statement just after except to do something if there was no founded error in the try block, else statement here is depending on except and except itself depends on try block.
We can have multiple except to handle deferent kinds of errors in the try block if we don’t write any error name after except keyword it will be used for any kind of error but we can make specific except blocks for specific types of errors for example TypeError, OSError, etc. :

                except TypeError:                            # handle only one type of errors
                                codes
                except:                                                 # handles all types of errors
                                codes

final:
final will be executed always either try finds an error or not, finally blocks depends on try block that means we can use it without try block.

I/O Stream

Input and output stream allow us to read and write from and to external files, in order to use I/O Stream on files we must open them first:

                VarName = open(‘Address//FileName.extension’ , mode = ‘Value’)

Note: modes for opening a files can be one of the bellow options:
·       
            mode=’r’             # read only
·         mode=’w’           # write only (overwrites the existing data)
·         mode=’a’            # write only (adds given data at the end of file)
·         mode=’r+’          # read and write
·         mode=’w+’        # read and write (overwrites the existing files or create a new file)

by default, the value for mode is ‘r’.

Reading from existing files:
If the variable that storing file have the reading ability we can read from a file like below that returns the values:

                VarName.read()

Note: we can write the ending index inside read prentices to provide a limit for reading, python will read up to index and the values will not include the index itself, once we read a file we must use seek method with zero index to bring back the calcar back into file beginning:

                VarName.seek(0)

Writing into existing file:
After we opened a file we can write in it according to mode of open if that not support the writing we will not be able to write into the file:

                VarName.write(‘value’)

Modules and packages

Modules:
Modules are simple python files (files with .py extension) in the same path of our python file that we are working with, we can use the modules variables, functions and classes inside the python file that we are working with by including them in the top of our file:

                from ModuleName import Element

in the above module name must be the second .py file name without any extension and the element must be one of the module variable, function or class name, we can also import all elements of the module by writing (*) asterisk instead of element after import.

Note: we can import the entire module and access its elements by putting a dot after its name:

                import ModuleName

Packages:
Packages are simple folders in the same directory which our main file exist. Theses folders needs an empty file by the name of (__int__.py) to make the python to threat them like a package not a simple folder, after that we can put our modules into the package to make our modules look cleaner and sorted, after creating packages and modules we can access the modules by the following:

                from PackageName import ModuleName

then we can access the module elements by putting a dot at the end of the module name in our main code.
We can even have packages inside packages and accessing them is quite like the normal package including with one main deference, we must use dot after the package name following by the sub package name at the including time:

                from PackageName.SubPackageName import ModuleName

Note: python have a built-in variable (__name__) that stores (‘__main__’) inside it if the file is running directly, we can see if the file is running directly or it is not in other words It is imported by checking the value if (__name__) using a simple if statement.

As:
When we are importing modules, itself we can import it by a name to use latter:

                from PackageName import ModuleName as Name

now we don’t need to write the module name to access its elements instead of that we can use the given name following by a dot to access the module elements.

No comments