[Autogenerated] now it's time to go through our first demo,
we'll start off by setting up a virtual environment and
get familiar with the starter script.
Then we'll install the python magic module using pip and will
leverage it to classify unknown falls 1st.
We'll set things up so I'll open Visual Studio Code but any text editor or I.
D will do I already have a couple of falls set up which will
explore in greater detail throughout this course,
VS Code has a terminal built into it.
So I launched a new one and check the python version on most
Linux distributions and macOS You might need to run python
three and then dash capital V.
I'm on Python 3.8.
So this is perfect to manage all dependencies.
I will make use of virtual environments but first I need to
install this via the apt package manager.
Once installed,
I can create a new virtual environment using Python three specify the
Vienna module and then the name Viene which will be the name of the newly
created virtual environment after it's created,
I can activate it by running the source command followed by the path Viene being
and activate and you can notice this in the terminal as well because the virtual
environment has been created with Python tree.
I can use the simple Python alias and it will still leverage Python tree.
A good practice is to update pip.
The python package manager by running the following
command pip install dash dash, upgrade pip.
I'll clear the terminal and I'll open the starter script as
you can notice RGBA parts is already set up.
Let's execute the script in the terminal by using the dash age argument.
This prints the help of the script.
Perfect.
Now we know everything works.
One of the important arguments that will be required for
our script is the path to either a fall or a directory
where scans can be performed.
So I'll add a new argument.
I'll call this path and also specify the text that
will be displayed in the help menu.
There might be some scenarios where the path might be invalid.
So next I will add some validation.
If a path does not exist,
then I want to print an error message and also exit the script.
So let's run this again with the dash age argument.
You can notice that the path argument is present to test our condition,
I'll specify an invalid path and I get the expected error message.
The next thing that we want to implement is the flexibility to either
scan a file or multiple files contained in a directory.
This can be achieved by treating falls within the directory as a
list of falls and if a single file is specified,
this should be treated as a list with one element for this.
I'll create a new function and this will be called get falls
and it will have a path as an argument.
Since the code will already check for the validity of the path.
I don't have to re implement this so we can move forward first.
I will check if the path is a directory using os dot path dot is the I R.
And then the path as the argument.
Then we'll create an empty list called falls.
I will use a for loop to iterate through all of the files in the
directory which I will retrieve using list D I R.
And then the path.
I will create a new variable to store the complete path and use always that
joint to combine the path to the directory with the file name.
Then I also have to check if the path is a foul.
Using os dot path dot is foul and add this path to the file list
outside of the for loop I will return the foulest.
On the other hand, if the path that is provided is a foul,
I will return this as a one item array.
Now let's implement this in the main program right
after the validation of the path,
I will create a variable called falls which will receive
the value returned by the get falls function to access the
path provided by the user.
I will use our XPath then I will iterate through all of the files and
print the path to make sure that everything works.
Let's test this out so I will call Python that name of the script and use the
samples directory and we can see it works now that we have the basic
functionality in place it's time to move to the next level.
I will be making use of the Python magic module to
extract information about the file type.
Please consider that this is an interface for the lip Magic library.
The documentation page contains a lot of important information,
including installation for the various operating systems.
I will continue and install python magic using pip to test this,
I will launch Python I will import the magic module,
I will also clear the screen and to get the type of a foul.
I will use magic dot from underscore foul and I will use this
inner print function and it works file one is identified as a P
32 executable for Microsoft windows.
Next let's implement this in the script as well.
First,
I have to add the magic module up at the top to be
imported any of the for loop below.
Each of the file names aren't also print the type as well.
Now I can run the script to test it and it works.
We can see all the files have been classified now we can move forward to the
next part and see how we can implement even more functionality