Table of Contents[Hide][Show]
This lecture will introduce you to some of the advanced Python concepts, like working with directories and Django.
Directories in Python
In Python 3 module index, you can see we have a module called pathlib
which provides object-oriented file system paths. It provides classes that we can use to create objects to work with directories and files.
If you open it you can see how this module works.
You can import the module like this:
from pathlib import Path
Path
is the class here. Now we need to create a path object to reference a file or directory on our computer. There are two ways to do this. We can use an absolute path or a relative path. Absolute path means that you will have to define the path from the root of the hard disk, while relative path means a path starting from the current directory.
Absolute paths in Windows and Mac are like c:\program files\windows\
and /usr/local/bin
respectively.
After defining the module and class, we need to create a path object.
Path()
If you don’t pass an argument here, this will reference the current directory. Alternatively, we can pass a string, in which we can add a file or a directory. let’s say the ecommerce
directory.
Path("eCommerce")
Now, this returns a path object. Let’s store it in the path
path = Path("eCommerce")
This path object has a few interesting methods, to begin with.
For example, we can use exist
method to check if a path exists. This returns a boolean so let’s print it on the terminal.
If the directory “eCommerce
” doesn’t exist, we will get a false. In that case, we can use the mkdir
method to make a directory. Let’s create a directory “email
” through this method.
from pathlib import Path
path = Path("email")
path.mkdir()
This returns nothing but the directory email
will be created in your project panel. We can also delete this directory through the rmdir
method, which is short for “remove directory”.
It is very useful to write a concise program to automate something. For example, you can iterate over all the spreadsheets in a directory, open them and process them. Let me show you.
To do this, switch to the default directory and use glob
method. This method can search for files and directories in the current path. We need to pass a string that defines a search pattern. We can type an asterisk which means everything, all files, and all directories. Optionally, an extension can be added. You can type ("*.*")
to get all the files in the current directory. We can also search all the excel files like this:
from pathlib import Path
path = Path()
print(path.glob("*.xls"))
Let’s search for all the .py files in a similar fashion.
Running the program will give a generator object. These are advanced Python concepts, but you need to know that we can iterate or loop through these generator objects. Instead of printing this generator object, let’s iterate over it using a for loop.
from pathlib import Path
path = Path()
for file in path.glob("*.py")):
print(file)
Let’s run this program:
It will list all the Python files in the current directory. Similarly, running the program with just an asterisk in the current path will list all the files in the current directory.
PyPi and Pip
Even though Python’s standard library is comprehensive and has several modules for common tasks, it’s not complete by any means. That’s why we have a directory called python package index or PyPI. In this directory, you can find hundreds of packages for doing awesome things. These are the packages that the people like you and me have built as part of our projects, and then they have published them as a part of this directory for others to use.
That’s the benefit of an amazing Python community, as there are a large number of python developers out there, and they have developed several packages for us to reuse in our programs.
Let’s say you want to send text messages in your programs. You won’t have to build this functionality from scratch, and you can come to pypi.org and search for SMS.
As you can see, hundreds of projects or packages give you this functionality. Not every package or project is complete or bug-free. Some of them are still in development, or they might have bugs, but if you look around for any function, you can imagine there is a good, reliable, well-documented package for you.
To install a package from pypi.org, you must type the command in the terminal window. Pip is a tool that comes with Python installation, and we use it to install or uninstall packages registered on pypi.org. Now we can import this package; its modules are just like the built-in modules in Python or the modules we have in our project.
Final Wrap-Up!
I hope you understand the basic concepts of Python at this point. You can now practice Python as much as you want, as “Practice makes a man perfect.”
I would suggest you master this course before learning advanced Python. This Python course can be extended to learn data analysis, machine learning, web development, or any other field in Python. I wish all of you the best in your programming career.
Keep coding!
Leave a Reply