In Python, or in any other language, you get the
core set of functions. For example, the print
function and the open functions are part of the
core Python functions. You can just use them like
that, and it's going to work. Now you have many
built in functions. But in the end, what you can
do with them is quite limited. If you want to do
more things, for example, you want to create or
you want to remove a folder, connect to a web
page, send an email, etc, etc, you need to write a
lot of code all by yourself. And this is going to
take you a long time. Fortunately for you, there
are many available libraries of Python modules
that you can use. The Python module is simply a
collection of plug and play functionalities to
solve a specific problem. And there are so many
Python modules that, of course, I won't show them
all, and you won't be able to know them all. But
here, I'm going to show you how you can import a
module and use its functionalities in your Python
code. And then you will be able to replicate this
for each new module you want to use. And first,
I'm going to also comment this. So now I have a
lot of commented line. So this is just as if the
file was empty. I'm going to simply do import os.
And I'm going to run that. And you can see
this is working. So we don't have any error. But
we also don't have anything on the screen. Okay,
so to import a module, you simply do import, and
then the name of the module, and then you can use
the module in your code. And best practice is, of
course, to put it at the beginning of your
program. So I'm going to put it right there. Okay,
even if I have commented lines, I'm going to put
it at the beginning of the program. In the os
module, what is the os module. The os module is
simply going to give you some new functionality to
interact with the os, and for example, to create
files, remove files, create directories, etc. And
now let's use this module, so this os module to do
something useful. Let's say we want know, to
remove the new file here that we have created
previously. How to remove that? What we can simply
do is, I'm going to write the code if os.path.exists,
we are first going to check if the
file exists actually, before we remove it. new_file,
I'm going to do os.remove with new_file.
So this is a function from os and actually,
you have a sub module named path. So os.path.exists
is going to tell you if the file
exists and return a Boolean. So if this is true,
you go inside the if and then you have a function,
os.remove, which is simply going to remove
a file. Here, you can give the relative path like
this or the absolute path like we did before. And
because we use that multiple times, I can also
create a variable, let's say file_name is equal to new_file
and just pass that variable here, which is
going to make it more convenient. Now let's run
that code, and let's look at the file here. Okay,
the file is gone, new_file is gone. Now if I run
it again, you can see so the file is still gone.
And we don't have any error because we first check
if the file exists before we try to remove it. If
you try to remove a file that doesn't exist, you
are going to get an error. So well, as you can see
that just the way of using a module. import name
of the module, and then you can use module dot
name of the function. And sometimes you have a
module inside a module. So module, so module
function, alright, and one thing you can also do
is instead of using import os, okay, which is
going to import all the os module, you can import
specific functions. So if you want to do that, you
can do from os import, for example, remove. This
is going to import the remove function from the os
module. And you can do from os.path import exist.
Okay, I'm going to remove this. And now what you
can do, you can simply use the exists and remove
functions like this. So if you directly import the
module like this. import os, you need to put os.remove,
but if you do from os import remove,
you simply need to just write remove like this,
and this is also going to work and with this way
you simply import two functions. You're gonna
import any other functions from the os module,
just what you need. And now well, basically how to
find that information, how to know that you need
to do os.path.exists, os.remove? Well,
I'm going to say to you, you need to search that
on Google. And that's what I do all the time,
that's what every Python developer do all the time
is to search on Google. And to easily search for a
module, you simply type, Python, the name of the
module, and then module, and you're going to get
to page so you can get to that page, docs.python.org.
Okay, and on the top, so you can even
choose the language and make sure that you have a
version that is at least Python three, and that
corresponds to the version you are using, okay?
And then you can see you have the complete
documentation for os. Okay, so if I search for
os.remove, you can see I have always let's
remove here, and you have all the information with
some examples, sometimes the parameters you need
to give, etc, etc. Alright, now you know how to
import and to use a python module in your program.
And you also know where you can find a complete
documentation for any given built in module. And
of course, you're not going to know all the
modules and all the functions that whenever you
have a problem, you just make a Google search and
you're going to see what kind of module you need
to use what kind of functions then you can read to
the condition so you can use that correctly.