- [Instructor] Sometimes when we're working with Python,
we want to work with an external file.
If we have a lot of data we're dealing with,
we keep using that example of an inventory list,
we might have hundreds or thousands
of items in the inventory,
we probably don't want to have to code
that into our Python script
and we probably don't want to have
the user enter it every time.
It would be so much more convenient
to use an external file to contain all that information.
And that's what we're going to look at here.
How to interact with an external file on my hard drive.
By the way, the file that I'm going to be interacting with
is called inventory.text and here's what it looks like.
It's got four items, and we're going to be able to read
each of these items from this text file.
And later in the video,
we'll see how we can add on to this file.
Now, in the background, I just closed that text file
after we work with it a bit and we add some stuff onto it,
we'll open it up again.
But for now, let's see how we can pull information
from that text file.
We're going to have a variable,
and I'm just going to call it file,
file's not a keyword saying we're dealing with a file,
I'm just calling our file, file.
And I'm going to say we're going to be opening the file
of inventory.txt and I need to put that in quotes.
And my external file.py, this Python script,
lives in the same directory as inventory.txt.
If it did not, I would need to add
some path information here.
I like to typically have my script and my file
in the same directory if that's practical to do.
Let's close quotes.
And when I'm working with this file,
I don't want to be destructive.
I just want to read information from it,
I don't want to write to it.
So, I'm going to open this up in a specific mode.
I'm going to open it up in read only mode.
To do that I'll do a comma and then an r in quotes,
meaning read-only mode, and closed parentheses.
By the way, we do have other modes.
There's a write mode that I could use,
but that's destructive.
If I already had an existing file,
if I opened this up in write mode,
if I did write something to it,
it would overwrite that existing file,
starting from scratch.
So, that could be destructive.
There's an append mode that we'll see later in this video
that is not destructive.
It allows us to add information onto the end of a file.
And there's also an R+ mode
that let's us do some reading and writing,
but here, we just want to be in read only mode.
And we're going to go through a for loop.
Just like a for loop could look
through the items in a list,
and when we ran out of items, we stopped the for loop,
we can look through items in this file,
treating it like a list.
I'm going to say for, and I'll use the variable of item,
which is going to be the value of a specific line
as we're looping through this,
so the first time we go through this loop,
the item is going to be that first item
that I had in the list.
The next time we go through the loop,
item is going to equal the second item I had in the list.
I'll say for item in the file that happens to be named file,
and we have to end this with a colon,
then I simply want to print the value of that item.
As long as we're looping through and finding new items,
we're going to keep printing them.
And as a good practice, we should usually close out
our file when we're done.
So, I've got this file that's named file,
and I want to close it,
so we say .close, open and closed parentheses.
I'm going to save this and I'm going to run it.
And it prints out those four items
that we saw a few moments ago in my text file.
Something's not perfect yet, though.
Notice that we've got some extra space here.
There's this extra hard return, it looks like.
We didn't have that in our original text file,
what's going on here?
Well, each of those lines in the text file
had a hard return at the end and when we say print,
that comes with its own hard return.
So, we have two hard returns causing that extra line.
There's a way to strip that off, though.
What we can do is strip off an extra hard return
or any white space at the end of a variable.
And here's how we do it.
I want to clean up the variable named item.
I want to get rid of that extra white space,
that extra hard return.
So, before we print it, let's add a line just above that.
Before we print it, I'm going to change the value of item
to equal item.strip, open and closed parentheses,
which is going to, again,
strip off the extra white space at the end.
Now, let's save this and run it.
We'll do a save and run module,
and that looks a lot cleaner this time.
Now, let's see how we can read these inventory items
from a file and then use it to populate a list variable
that we can then work with, as we've been doing
in previous videos.
Let's say that we're going to have this list variable
named inventory, and we want to populate it
with the values in that file that we're going to be reading in.
Just above our first line here,
let's add a line where we're going to be defining
a list variable named inventory,
and it's empty to begin with,
so I'll say it equals open and closed brackets,
and we're still going to be opening this file in read mode.
We're going to be looping through, as long as we have an item
in here, we're going to be looping through,
and the item is going to equal each of those lines in the file.
We're going to be stripping off that extra hard return,
but instead of printing the item,
I want to add those items to the list variable of inventory.
To do that, I say inventory.append
with the value of whatever item is right now,
and we'll close it like we did before.
When we're done, let's say we want to print the value
of inventory, just to make sure that it worked correctly.
Let's save and run this.
That looks great.
These are the items in our inventory,
and now they belong to this list variable.
We can see the open and closed brackets,
and the comma separated values.
Now, let's take a look at how we can write to a file.
When I do this writing, I don't want to be destructive
to my original inventory file,
so instead of using write mode,
which would start from scratch
and erase anything I had existing in that file name.
Instead of using the write mode,
I'm going to use the append mode,
and I want to start from scratch.
So, let's delete what we have here.
We'll just again, begin from scratch,
and I'll say that I've got this variable named file,
and it's going to be the inventory.txt file specifically.
I'm going to open the file,
and the file name is inventory.txt comma.
This time I need to open an append mode
because I'm going to be adding content to this file.
I'll say a in quotes, closed parentheses,
and now I need to create a loop.
I need to loop through here asking the user each time,
"You want to enter another inventory item?
"Do you want to enter another item?"
We're just going to keep looping through until they're done.
I don't know how many times they're going to enter,
I want to create a while loop that's always true.
I'll say while True, and true is always true,
and we'll say colon.
While this is true, we want to prompt the user
to enter a new inventory item, and I'll say,
we've got this variable called newItem,
and it's going to equal whatever the user inputs.
We'll say we're going to prompt them with the string,
Enter new inventory item
colon, space closed quote, closed parentheses.
We need to check to see if they're done
and we're going to tell the user to enter a keyword
of exit if they're done, and we need to check and see
if they entered that keyword.
Let's do a little check using the if logic.
I'm going to say if the value of newItem equals exit,
and we have to use a double equal sign here
because we're doing an evaluation, a boolean operation.
If it equals the string of exit, close quote,
and we know that an if statement has a colon at the end.
If the new item equals exit, then we want to print
the string of All done!,
closed quote, closed parentheses,
and then we want to break out of that loop.
If that's not the case, if the new item does not equal exit,
then we want to take whatever they typed in,
and append it to our file.
We want to write to the file,
and we're not going to be destructive because again,
we're using the append mode when we open the file,
but here's how we write to the file.
I'm going to give the variable of file,
and I'm going to use a .write.
We're writing in append mode, so we're not erasing anything.
I want to write the value of newItem,
whatever the user typed in, but we have an issue here.
Remember earlier, we had too many hard returns.
We had two of them.
Well now, we don't have any,
and if I don't enter a hard return,
everything's just going to sort of be stuck
side-by-side with each other when I enter these new items.
I do want to enter a hard return.
The way I can do that is say I want to add in quotes
\n closed quote, closed parentheses,
and it's a good practice to close our file.
So, I'm going to say file, which is just again,
the name of the file.
It's not some sort of a special file keyword.
I'll say file close, open and closed parentheses.
Let's save this.
Let's run it, and it says, "What is your
"new inventory item?"
Let's say that I want to add a new phone.
I'm going to say
Cisco 7945G IP phone.
Let's add another one.
I'll say this is a Cisco Unity Express Module,
and now let's say that I'm finally done,
and I'll say exit.
It's going to catch that, hopefully in that logic,
and it's going to say, as it does, All done!
Now, I'm going to open up that text file,
in fact, I just opened it up in the background.
Let's go take a look at it.
Here is that inventory.txt file
we were dealing with earlier.
Look at this.
We've got two new items that we appended
to the original file.
This is the last video in this Python section,
not because Python doesn't have more features.
Python certainly has lots of twists and turns
that we haven't discussed, but here's what I've discovered.
In most cases, it's better to introduce you
to a fundamental set of tools that we can build on,
rather than trying to give you all things Python
in this section because it's going to be difficult
to memorize all that when you go out
and try to do some network programming.
Instead, I'd much rather equip you
with a fundamental set of tools,
and we can start applying these concepts to programs
that we write to control network devices.
Sure, we'll occasionally need to learn
another Python feature,
but think about what you've learned already.
You've learned how to install Python,
how to work with the interactive interpreter,
how to use IDLE.
We talked about various data types,
how we can convert between data types.
We contrasted lists with dictionaries.
We saw how to get input from a user.
We looked at the if function, the for loop,
and the while loop.
In this video, we talked about working with external files.
That's a great set of fundamental tools
that we can use to go forward
in our network programmability journey.