WEBVTT

1
00:00.000 --> 00:04.330
Now that we have some of the basics of Python under our built, let's now take a

2
00:04.330 --> 00:04.880
look at

3
00:04.880 --> 00:06.880
writing some Python scripts.

4
00:06.880 --> 00:10.260
Now one of the most important things on one of the most valuable things that

5
00:10.260 --> 00:10.800
you can do

6
00:10.800 --> 00:15.560
with Python is having the ability to read in particular files.

7
00:15.560 --> 00:16.560
What could this actually entail?

8
00:16.560 --> 00:21.000
We'll say for example, if you want to be doing network automation, for example,

9
00:21.000 --> 00:21.040
you may

10
00:21.040 --> 00:25.340
want to read in some type of configuration file and then use Python to send

11
00:25.340 --> 00:26.000
that file

12
00:26.000 --> 00:27.840
to the target device.

13
00:27.840 --> 00:31.560
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

13
00:27.840 --> 00:31.560
Similarly, maybe you want to work with particular devices that are defined in a

14
00:31.560 --> 00:32.200
particular

15
00:32.200 --> 00:33.800
inventory file.

16
00:33.800 --> 00:38.460
Once again, if you want to read in that file, well, it's obviously going to be

17
00:38.460 --> 00:39.080
helpful

18
00:39.080 --> 00:40.720
to know how to do that.

19
00:40.720 --> 00:44.070
So before we actually do some of the fun stuff and begin connecting to devices

20
00:44.070 --> 00:44.560
and making

21
00:44.560 --> 00:49.560
changes to those devices, let's just first focus in on the ability to open up

22
00:49.560 --> 00:50.000
particular

23
00:50.000 --> 00:53.480
files as well as how we can read the contents.

24
00:53.480 --> 00:54.480
Sound good?

25
00:54.480 --> 00:55.960
Well, let's go ahead and do it then.

26
00:55.960 --> 00:59.120
So to begin, let me just create a new directory to keep things nice and clean.

27
00:59.120 --> 01:02.120
Let's just call this maybe Python scripts, make a sense.
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

27
00:59.120 --> 01:02.120
Let's just call this maybe Python scripts, make a sense.

28
01:02.120 --> 01:06.330
Let's go into this directory here and inside, let me just create an example

29
01:06.330 --> 01:07.280
called example

30
01:07.280 --> 01:08.280
1.py.

31
01:08.280 --> 01:10.760
Okay, so let's open this up right here.

32
01:10.760 --> 01:14.510
So you may remember in the previous skill we talked about functions and we know

33
01:14.510 --> 01:14.880
that

34
01:14.880 --> 01:18.920
Python has some built-in functions such as the print function.

35
01:18.920 --> 01:23.080
In this particular case, we're going to be using another built-in function.

36
01:23.080 --> 01:26.700
This one though is known as the open function and you can maybe guess what it's

37
01:26.700 --> 01:27.440
going to do.

38
01:27.440 --> 01:30.400
It's going to help us open up particular files.
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

38
01:27.440 --> 01:30.400
It's going to help us open up particular files.

39
01:30.400 --> 01:33.720
Okay, so to begin, in fact, what I should do is actually have some type of text

40
01:33.720 --> 01:34.200
file to

41
01:34.200 --> 01:35.200
read in.

42
01:35.200 --> 01:37.000
Let's just call this readme.txt.

43
01:37.000 --> 01:40.220
Okay, if we're going to read files, it does help that we have one to actually

44
01:40.220 --> 01:40.680
read.

45
01:40.680 --> 01:42.400
So let's try this then.

46
01:42.400 --> 01:45.080
Let's just say my name is John.

47
01:45.080 --> 01:47.320
This is just an example.

48
01:47.320 --> 01:49.120
I love Python.

49
01:49.120 --> 01:53.200
Okay, so nothing too interesting in this particular file, but nevertheless we

50
01:53.200 --> 01:55.000
want to read the contents.

51
01:55.000 --> 01:56.400
Let's see how we can do this.

52
01:56.400 --> 02:01.130
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

52
01:56.400 --> 02:01.130
Now first thing to note, the name of the file we have right here is read_hyphen

53
02:01.130 --> 02:02.000
_me.txt.

54
02:02.000 --> 02:03.000
This is going to be important.

55
02:03.000 --> 02:06.660
We're going to have to reference the name of the file that we want Python to

56
02:06.660 --> 02:07.200
actually

57
02:07.200 --> 02:08.200
open.

58
02:08.200 --> 02:11.720
So if we go back to our script right here, so the first thing I will do is

59
02:11.720 --> 02:12.440
create a little

60
02:12.440 --> 02:13.440
variable.

61
02:13.440 --> 02:17.030
So call this anything you wish and we'll just call it maybe target_file,

62
02:17.030 --> 02:17.640
something

63
02:17.640 --> 02:19.240
fairly descriptive.

64
02:19.240 --> 02:22.260
And now what I will do is I will use the open function, like I say, it's a

65
02:22.260 --> 02:23.240
built-in function.

66
02:23.240 --> 02:25.760
It's going to be accessible to all users of Python.

67
02:25.760 --> 02:29.900
And now what I will do is I'm just going to specify the name of the file that I

68
02:29.900 --> 02:30.360
want to
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

68
02:29.900 --> 02:30.360
want to

69
02:30.360 --> 02:31.360
open.

70
02:31.360 --> 02:34.400
Now we can actually see here within the same directory.

71
02:34.400 --> 02:35.400
This is the name of the file.

72
02:35.400 --> 02:37.160
So let me just copy this right here.

73
02:37.160 --> 02:41.610
And if I just paste this in, what I will now do is have a comma and immediately

74
02:41.610 --> 02:42.080
after

75
02:42.080 --> 02:47.060
I'm going to use the letter R. Pretty much all we're telling here, or rather

76
02:47.060 --> 02:47.240
all we're

77
02:47.240 --> 02:52.680
telling the open function is that we want to read this particular file.

78
02:52.680 --> 02:56.180
Whereas if you want to write into the file, we would use the keyword W. But in

79
02:56.180 --> 02:56.520
our case

80
02:56.520 --> 02:58.200
here, we simply want to read.

81
02:58.200 --> 03:02.510
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

81
02:58.200 --> 03:02.510
So what this effectively does is that it opens the contents of this file in

82
03:02.510 --> 03:03.200
read mode

83
03:03.200 --> 03:07.600
and it saves it to a Python object, a variable known as target_file.

84
03:07.600 --> 03:11.040
To check this out right now, if I now print target_file, watch what actually

85
03:11.040 --> 03:11.640
happens and

86
03:11.640 --> 03:13.400
it may surprise you a little bit here.

87
03:13.400 --> 03:16.000
So let's run it, hit enter, look at this right here.

88
03:16.000 --> 03:19.680
We're not actually seeing the content of the text rather instead.

89
03:19.680 --> 03:24.760
We're just seeing that we have this type of object, this, this text object.

90
03:24.760 --> 03:29.430
So to speak, in fact, if I happen to print the type, we can see here it is a

91
03:29.430 --> 03:30.240
text input
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

91
03:29.430 --> 03:30.240
text input

92
03:30.240 --> 03:31.240
output wrapper.

93
03:31.240 --> 03:35.120
Okay, so it's not exactly the actual text that we want to be looking at.

94
03:35.120 --> 03:37.900
Now just before we actually go and read the content of the file, quite

95
03:37.900 --> 03:39.120
important here,

96
03:39.120 --> 03:43.400
what has actually happened here at this stage, I have opened the file, okay?

97
03:43.400 --> 03:47.040
And it's now available within this particular object right here and all we're

98
03:47.040 --> 03:47.600
doing here

99
03:47.600 --> 03:51.720
is printing this at this stage here, the file is still open.

100
03:51.720 --> 03:55.380
So what we want to do in order to make our program run nice and smooth is we

101
03:55.380 --> 03:56.000
also want

102
03:56.000 --> 04:00.710
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

102
03:56.000 --> 04:00.710
to have the ability to close that particular file because if we don't close it,

103
04:00.710 --> 04:00.840
well,

104
04:00.840 --> 04:04.160
a program can be buggy, things might not operate as expected.

105
04:04.160 --> 04:05.680
So it's a good idea to also do that.

106
04:05.680 --> 04:09.430
So what I can do is once I am done with the actual file, all I will do is

107
04:09.430 --> 04:10.960
simply say the

108
04:10.960 --> 04:14.830
name of the object I've created and I'm going to say dot, close and then use

109
04:14.830 --> 04:15.760
parentheses.

110
04:15.760 --> 04:19.920
This is the close method that is ultimately going to close that file back down.

111
04:19.920 --> 04:23.290
Now right now, if I run it, it looks like nothing has changed, but pretty much

112
04:23.290 --> 04:23.720
we have

113
04:23.720 --> 04:27.480
open to file, read the type of the file and then close it back down again.

114
04:27.480 --> 04:30.890
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

114
04:27.480 --> 04:30.890
Now just to be clear here, there is one other way in which you can do a similar

115
04:30.890 --> 04:31.360
thing and

116
04:31.360 --> 04:36.080
it actually is probably an easier way or a more elegant way to do so.

117
04:36.080 --> 04:38.920
And this is utilising something known as a context manager.

118
04:38.920 --> 04:42.320
This is something you actually see quite a lot within Python scripts.

119
04:42.320 --> 04:46.870
Okay, quite simply a context manager is just going to use special syntax and

120
04:46.870 --> 04:47.640
when we happen

121
04:47.640 --> 04:52.240
to open a particular file, the context manager will automatically close it once

122
04:52.240 --> 04:52.920
we are done

123
04:52.920 --> 04:53.920
with it.

124
04:53.920 --> 04:57.680
So we don't actually have to be quite as explicit and specify it manually here.

125
04:57.680 --> 05:00.680
Okay, so let's go and try to use that particular syntax.
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

125
04:57.680 --> 05:00.680
Okay, so let's go and try to use that particular syntax.

126
05:00.680 --> 05:04.540
Now the way we specify context manager is we use the keyword with and then I

127
05:04.540 --> 05:05.240
can just use

128
05:05.240 --> 05:08.550
the open function, specify the name of the file that I want to open just like

129
05:08.550 --> 05:08.880
we did

130
05:08.880 --> 05:12.320
before and again we want to be opening this in read mode.

131
05:12.320 --> 05:16.000
And then I use the keyword as and now this is where I give the name of my

132
05:16.000 --> 05:16.920
variable, so let's

133
05:16.920 --> 05:19.240
just say as target file.

134
05:19.240 --> 05:23.280
Then I have a colon and then I hit return now indented within.

135
05:23.280 --> 05:28.080
This is where I perform all of my operations such as printing the target file.

136
05:28.080 --> 05:32.810
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

136
05:28.080 --> 05:32.810
Now as soon as I go here and I outdance automatically, this particular file is

137
05:32.810 --> 05:33.720
going to be closed

138
05:33.720 --> 05:36.040
so it's going to be handled a little bit more elegantly.

139
05:36.040 --> 05:37.880
So let's just try this again, run it.

140
05:37.880 --> 05:41.200
So we can see right here we have the actual file itself, but once again we're

141
05:41.200 --> 05:41.880
not reading

142
05:41.880 --> 05:42.880
the actual contents.

143
05:42.880 --> 05:47.220
So with the context manager, anything indented within is going to give us

144
05:47.220 --> 05:48.160
access to the open

145
05:48.160 --> 05:49.160
file.

146
05:49.160 --> 05:53.190
That means that within this indented block also we can perform actions to read

147
05:53.190 --> 05:53.840
the actual

148
05:53.840 --> 05:56.800
content within this particular object.

149
05:56.800 --> 06:00.170
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

149
05:56.800 --> 06:00.170
Now the way we're going to do this is we're going to utilize a particular

150
06:00.170 --> 06:01.000
method or rather

151
06:01.000 --> 06:02.000
two particular methods.

152
06:02.000 --> 06:06.480
Okay, so what I will do here is I'm going to create another variable yet again.

153
06:06.480 --> 06:11.040
Let's just say my data and I'll make that equal to and I'm going to grab the

154
06:11.040 --> 06:11.840
target file

155
06:11.840 --> 06:16.880
object that represents the open file and I'm going to call the read method on

156
06:16.880 --> 06:17.520
it case.

157
06:17.520 --> 06:21.080
This is why we have a parentheses and basically this is going to read the

158
06:21.080 --> 06:22.240
contents of the actual

159
06:22.240 --> 06:26.040
target objects and then save it into the my data variable.

160
06:26.040 --> 06:29.440
So this variable here actually has the real text within it.

161
06:29.440 --> 06:32.760
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

161
06:29.440 --> 06:32.760
So what I can now do is I can just print out my data.

162
06:32.760 --> 06:37.320
Like I say, we out-dent back out, we have an automatic closing of the file.

163
06:37.320 --> 06:40.360
Let's clear the screen and arrow up, run the script.

164
06:40.360 --> 06:41.360
Look at this right here.

165
06:41.360 --> 06:44.560
We're now actually reading the content of that particular file.

166
06:44.560 --> 06:49.190
Now one thing just to note here, if once again we happen to print the type of

167
06:49.190 --> 06:49.720
the actual

168
06:49.720 --> 06:53.280
information, save this and we run this.

169
06:53.280 --> 06:57.480
What we're actually receiving here is one big string blob of text.

170
06:57.480 --> 07:00.560
Now say for example, this was some type of configuration file here.
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

170
06:57.480 --> 07:00.560
Now say for example, this was some type of configuration file here.

171
07:00.560 --> 07:02.080
So if I go and modify this.

172
07:02.080 --> 07:08.030
For example, say router bgp 65001 and then we can say bgp router dash id and

173
07:08.030 --> 07:08.640
maybe give

174
07:08.640 --> 07:09.640
it all the ones.

175
07:09.640 --> 07:18.000
Add on a neighbor statement of 192 168 1.1 and say the remote as 6502 and let's

176
07:18.000 --> 07:18.680
also add

177
07:18.680 --> 07:24.640
the networking of 172 16000 with a mask of slash 16.

178
07:24.640 --> 07:27.910
So here's the thing, if we happen to read in this data one more time, we have

179
07:27.910 --> 07:28.480
what looks

180
07:28.480 --> 07:33.480
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

180
07:28.480 --> 07:33.480
like valid Cisco CLI syntax, but it actually isn't quite just yet because like

181
07:33.480 --> 07:34.080
I say, this

182
07:34.080 --> 07:36.680
happens to be one big string blob.

183
07:36.680 --> 07:40.870
When we want to send these commands to a target device, we would like to send

184
07:40.870 --> 07:41.680
them line by

185
07:41.680 --> 07:44.960
line, not as just one big single message.

186
07:44.960 --> 07:49.050
We'd like to say, you know, this particular configuration first, then hit

187
07:49.050 --> 07:50.000
return, then we

188
07:50.000 --> 07:54.960
go to this particular configuration, then hit return, same for this one and

189
07:54.960 --> 07:55.480
this one

190
07:55.480 --> 07:57.520
until we have no more lines left.

191
07:57.520 --> 08:02.480
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

191
07:57.520 --> 08:02.480
So essentially, we want to break up this information into a list so that each

192
08:02.480 --> 08:03.680
element in the list

193
08:03.680 --> 08:05.480
is its own line.

194
08:05.480 --> 08:06.960
So let me show you how to do this.

195
08:06.960 --> 08:11.440
As opposed to using the read method, we can use the read lines method and this

196
08:11.440 --> 08:11.920
is going

197
08:11.920 --> 08:14.320
to chop things up, line by line by line.

198
08:14.320 --> 08:18.840
If I just happen to run this game, look, it looks a little bit different.

199
08:18.840 --> 08:21.080
Now notice the square brackets on the outside.

200
08:21.080 --> 08:22.400
See that right there?

201
08:22.400 --> 08:25.100
Remember in the previous skill we talked about this, this is happens to be a

202
08:25.100 --> 08:26.200
list object

203
08:26.200 --> 08:30.000
and these backslash ends are ultimately new link characters.
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

204
08:30.000 --> 08:32.160
Basically, this is like pressing the return key.

205
08:32.160 --> 08:36.220
So now the actual data we're getting back here is a list object and what we can

206
08:36.220 --> 08:36.600
do now

207
08:36.600 --> 08:39.320
is loop through each element within the list.

208
08:39.320 --> 08:42.950
So I can say for command in my data and right now all I would do would be to

209
08:42.950 --> 08:43.920
simply print

210
08:43.920 --> 08:45.840
out each command one by one by one.

211
08:45.840 --> 08:50.240
So if I clear the screen and run this here, this is one command on its own

212
08:50.240 --> 08:51.080
followed by

213
08:51.080 --> 08:53.920
the next command, followed by the next followed by the next.

214
08:53.920 --> 08:58.000
As opposed to having one big continual stream of text data.

215
08:58.000 --> 09:00.960
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

215
08:58.000 --> 09:00.960
So these are the different ways in which we can open files and read files.

216
09:00.960 --> 09:04.320
It is very, very common to see this within network automation scripts.

217
09:04.320 --> 09:08.490
But another useful utility is also having the ability to read in particular

218
09:08.490 --> 09:09.240
file formats

219
09:09.240 --> 09:12.800
such as say, for example, the YAML file format.

220
09:12.800 --> 09:15.000
So let's talk about how to do that in the very next video.
