Now that we have some of the basics of Python under our built, let's now take a
look at
writing some Python scripts.
Now one of the most important things on one of the most valuable things that
you can do
with Python is having the ability to read in particular files.
What could this actually entail?
We'll say for example, if you want to be doing network automation, for example,
you may
want to read in some type of configuration file and then use Python to send
that file
to the target device.
Similarly, maybe you want to work with particular devices that are defined in a WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
Similarly, maybe you want to work with particular devices that are defined in a
particular
inventory file.
Once again, if you want to read in that file, well, it's obviously going to be
helpful
to know how to do that.
So before we actually do some of the fun stuff and begin connecting to devices
and making
changes to those devices, let's just first focus in on the ability to open up
particular
files as well as how we can read the contents.
Sound good?
Well, let's go ahead and do it then.
So to begin, let me just create a new directory to keep things nice and clean.
Let's just call this maybe Python scripts, make a sense. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
Let's just call this maybe Python scripts, make a sense.
Let's go into this directory here and inside, let me just create an example
called example
1.py.
Okay, so let's open this up right here.
So you may remember in the previous skill we talked about functions and we know
that
Python has some built-in functions such as the print function.
In this particular case, we're going to be using another built-in function.
This one though is known as the open function and you can maybe guess what it's
going to do.
It's going to help us open up particular files. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
It's going to help us open up particular files.
Okay, so to begin, in fact, what I should do is actually have some type of text
file to
read in.
Let's just call this readme.txt.
Okay, if we're going to read files, it does help that we have one to actually
read.
So let's try this then.
Let's just say my name is John.
This is just an example.
I love Python.
Okay, so nothing too interesting in this particular file, but nevertheless we
want to read the contents.
Let's see how we can do this.
Now first thing to note, the name of the file we have right here is read_hyphen WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
Now first thing to note, the name of the file we have right here is read_hyphen
_me.txt.
This is going to be important.
We're going to have to reference the name of the file that we want Python to
actually
open.
So if we go back to our script right here, so the first thing I will do is
create a little
variable.
So call this anything you wish and we'll just call it maybe target_file,
something
fairly descriptive.
And now what I will do is I will use the open function, like I say, it's a
built-in function.
It's going to be accessible to all users of Python.
And now what I will do is I'm just going to specify the name of the file that I
want to WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
want to
open.
Now we can actually see here within the same directory.
This is the name of the file.
So let me just copy this right here.
And if I just paste this in, what I will now do is have a comma and immediately
after
I'm going to use the letter R. Pretty much all we're telling here, or rather
all we're
telling the open function is that we want to read this particular file.
Whereas if you want to write into the file, we would use the keyword W. But in
our case
here, we simply want to read.
So what this effectively does is that it opens the contents of this file in WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
So what this effectively does is that it opens the contents of this file in
read mode
and it saves it to a Python object, a variable known as target_file.
To check this out right now, if I now print target_file, watch what actually
happens and
it may surprise you a little bit here.
So let's run it, hit enter, look at this right here.
We're not actually seeing the content of the text rather instead.
We're just seeing that we have this type of object, this, this text object.
So to speak, in fact, if I happen to print the type, we can see here it is a
text input WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
text input
output wrapper.
Okay, so it's not exactly the actual text that we want to be looking at.
Now just before we actually go and read the content of the file, quite
important here,
what has actually happened here at this stage, I have opened the file, okay?
And it's now available within this particular object right here and all we're
doing here
is printing this at this stage here, the file is still open.
So what we want to do in order to make our program run nice and smooth is we
also want
to have the ability to close that particular file because if we don't close it, WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
to have the ability to close that particular file because if we don't close it,
well,
a program can be buggy, things might not operate as expected.
So it's a good idea to also do that.
So what I can do is once I am done with the actual file, all I will do is
simply say the
name of the object I've created and I'm going to say dot, close and then use
parentheses.
This is the close method that is ultimately going to close that file back down.
Now right now, if I run it, it looks like nothing has changed, but pretty much
we have
open to file, read the type of the file and then close it back down again.
Now just to be clear here, there is one other way in which you can do a similar WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
Now just to be clear here, there is one other way in which you can do a similar
thing and
it actually is probably an easier way or a more elegant way to do so.
And this is utilising something known as a context manager.
This is something you actually see quite a lot within Python scripts.
Okay, quite simply a context manager is just going to use special syntax and
when we happen
to open a particular file, the context manager will automatically close it once
we are done
with it.
So we don't actually have to be quite as explicit and specify it manually here.
Okay, so let's go and try to use that particular syntax. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
Okay, so let's go and try to use that particular syntax.
Now the way we specify context manager is we use the keyword with and then I
can just use
the open function, specify the name of the file that I want to open just like
we did
before and again we want to be opening this in read mode.
And then I use the keyword as and now this is where I give the name of my
variable, so let's
just say as target file.
Then I have a colon and then I hit return now indented within.
This is where I perform all of my operations such as printing the target file.
Now as soon as I go here and I outdance automatically, this particular file is WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
Now as soon as I go here and I outdance automatically, this particular file is
going to be closed
so it's going to be handled a little bit more elegantly.
So let's just try this again, run it.
So we can see right here we have the actual file itself, but once again we're
not reading
the actual contents.
So with the context manager, anything indented within is going to give us
access to the open
file.
That means that within this indented block also we can perform actions to read
the actual
content within this particular object.
Now the way we're going to do this is we're going to utilize a particular WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
Now the way we're going to do this is we're going to utilize a particular
method or rather
two particular methods.
Okay, so what I will do here is I'm going to create another variable yet again.
Let's just say my data and I'll make that equal to and I'm going to grab the
target file
object that represents the open file and I'm going to call the read method on
it case.
This is why we have a parentheses and basically this is going to read the
contents of the actual
target objects and then save it into the my data variable.
So this variable here actually has the real text within it.
So what I can now do is I can just print out my data. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
So what I can now do is I can just print out my data.
Like I say, we out-dent back out, we have an automatic closing of the file.
Let's clear the screen and arrow up, run the script.
Look at this right here.
We're now actually reading the content of that particular file.
Now one thing just to note here, if once again we happen to print the type of
the actual
information, save this and we run this.
What we're actually receiving here is one big string blob of text.
Now say for example, this was some type of configuration file here. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
Now say for example, this was some type of configuration file here.
So if I go and modify this.
For example, say router bgp 65001 and then we can say bgp router dash id and
maybe give
it all the ones.
Add on a neighbor statement of 192 168 1.1 and say the remote as 6502 and let's
also add
the networking of 172 16000 with a mask of slash 16.
So here's the thing, if we happen to read in this data one more time, we have
what looks
like valid Cisco CLI syntax, but it actually isn't quite just yet because like WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
like valid Cisco CLI syntax, but it actually isn't quite just yet because like
I say, this
happens to be one big string blob.
When we want to send these commands to a target device, we would like to send
them line by
line, not as just one big single message.
We'd like to say, you know, this particular configuration first, then hit
return, then we
go to this particular configuration, then hit return, same for this one and
this one
until we have no more lines left.
So essentially, we want to break up this information into a list so that each WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
So essentially, we want to break up this information into a list so that each
element in the list
is its own line.
So let me show you how to do this.
As opposed to using the read method, we can use the read lines method and this
is going
to chop things up, line by line by line.
If I just happen to run this game, look, it looks a little bit different.
Now notice the square brackets on the outside.
See that right there?
Remember in the previous skill we talked about this, this is happens to be a
list object
and these backslash ends are ultimately new link characters. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
Basically, this is like pressing the return key.
So now the actual data we're getting back here is a list object and what we can
do now
is loop through each element within the list.
So I can say for command in my data and right now all I would do would be to
simply print
out each command one by one by one.
So if I clear the screen and run this here, this is one command on its own
followed by
the next command, followed by the next followed by the next.
As opposed to having one big continual stream of text data.
So these are the different ways in which we can open files and read files. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
So these are the different ways in which we can open files and read files.
It is very, very common to see this within network automation scripts.
But another useful utility is also having the ability to read in particular
file formats
such as say, for example, the YAML file format.
So let's talk about how to do that in the very next video.