Great, in this lecture you're going to learn how to open a file with Python and
how to read the file content so that you can manipulate that content there in
Python later on. For instance you may need some data, you may need to read some
data out of a file. Let's say that would be a file where you have weather
observation data and you want to open that with Python and then you want to
send an email with python to let's say your email address automatically,
so python needs to read the content of that file as a string and then it sends that
string as an email to your email address so that's just a random example,
If you're an experienced programmer you may find it easy to wrap your mind around
how to use file handling techniques so about real-world scenarios. If you're not
a programmer will be using file handling techniques in real-world examples, so in
the application that will be building. Nothing to worry about. So let's look at the
syntax than. Python can be used to handle both binary and text files. Text files
are files with txt extension such as this one here, so example dot txt and you
can open such files with editors such as atom and we also have binary files such
as this dll file in here which you cannot read with an editor as you can see,
but mostly you'll find yourself working with text files, so here is a
simple text file and what I'm going to do now is I'd like to read base three lies
in Python and print them out on the Python shell, so printing is crucial
when you want to test things out. Let's say we're about to write a program as I
said that sends an email but first of all you want to take things step by step
so you want to make sure that you're reading this file correctly with Python.
And to make sure that you're reading that correctly, you want to print that out.
You want to print the output in the shell. Once you see that the printing is
working correctly then you go to the next step and instead
of printing out that string you take this tree and pass it to your function
that produces, that sends emails, so it's good to do things one at a time and now
I can either go ahead and create a Python file where I write the code to
read this file or I can use the interactive shell. For the sake of this
demonstration of prefer to use the interactive shell, so that will allow me
to show you the intermediate results. So I write a line of code and I
show you something and then I'll write the other line and so on.
So yeah let's go ahead and one thing terminal plus which is the atom plugin
that allows me to access the command line from within atom is having a bug and I
cannot install it at the moment which is not a problem at all
because I can use the Windows command line. So shift right click and open
command window here. That will open the command line in
the current directory which is this one here, so Dropbox pp files. Here is where I
have the example dot txt file that I'll be working on and yep terminal plus
is good because you have everything in one window and it's especially good for
me up to make this video so that I have things organized, but using the terminal
of your computer is essentially the same thing, so to read the content of a file
with Python you first need to open that file using the open method and then
you want to read the file content using the read method, so first of all you want
to create a variable. Let's say file and then you use the open method which is a
built in method and then you point to the file that you want to open and there
is one more argument there you need to pass through open methods, so the
first argument is the file input and then you need to pass the mode that
you want to open that file and R means read. So the most important modes are
R and W. R is read and W is write. So let's focus on R now.
If you execute that, you'll get an error because I haven't opened Python yet.
And yes, sorry about that. Let me write that again, so I was writing
code in the command line and R. Execute. That was successful. As long as you
don't get an error that is successful, so what is file? If you check what file is
you'll see that it's a text file wrapper. Or you can call it a file object so what
we did is we imported that file in Python form by creating a special object in
Python and remember I told you that Python opens files as other programs do,
so if you try now to delete example.txt you'll get a message that the action
can't be completed because the file is open in Python, so once you
have opened that file you may want to read it and to read the content of the file
and it's wise to save a content in a variable, so I'm calling this variable
content and then I'll point to the file object which is saved in the file
variable and then to the read method just like that and execute. Nothing
happened so that was successful and now if you print content, you'll get the text of
the file which is actually a plain Python string. Sometimes though you may want to
save the lines in a list so instead of using Reads you want to use Read lines and
if you print content now you get an empty list which I'm sure it's not what you
were expecting, probably what you're expecting was a list with three items so
line 1 and line 2 and line 3. The reason we didn't get that is that you know when
you apply the read method, before applying the read method there's a
concept called the pointer, so before applying here the read method the pointer
is before the first character of the text file so just here and when you apply
the read method what Python does is it reads everything that is from that
pointer and down and so when the read
method finishes, so after the read method, the pointer has gone at this
point, so the pointer is here read method takes it here and at that point when you
apply another read method or in the Read lines as we did here, Python is
trying to read everything that is from this pointer and down and there is
nothing under this point so that's why we go this empty list the solution to
that is to apply a seek method there with an argument of 0.
So what that will do it will place the pointer back at the zero position of the
text file, so if you execute that you get a message from Python which you don't
need, then if you apply again Reads lies and then print content, this time you
get a list of three elements so line 1, line 2, line 3. That's what the seek method does.
And I know at this point it's hard to imagine why would you use the seek method,
but in one of our applications where we're blocking website, destroying
websites in our browser so that we cannot access those websites which
is good for productivity, in that application we'll be using the seek
method and there you'll understand how that comes into play, so it's actually very
useful in certain scenarios. It really gives you a lot of control over the files.
Yeah, back to this list and you see this backslash n characters there.
So one here, and another here and yet third one here and backslash n in Python means a
new line so, we have here line one. But then when I created this text file I did
it like I wrote line one and then I press enter, so in other words enter is
the backslash n and that is useful when you want to write content in a text file,
so we'll be using backslash later in the next lectures to write content in
different in separate lines in a text file and to get rid of those characters, to
end up with a clean list with line 1, line 2, line 3
there may be different walk arounds there but what I'm thinking of is
a list comprehension, so our existing list is content and you can either create a
new list, let's say content 1, a new variable to store the new list,
the clean list or you can update your existing valuables by passing
content there and that would be equal to a new list
so now you want to create this new least on the fly using a list comprehension,
so that would be rstrip which is a method that remove some characters
from a string and in this case we would want to remove backslash n,
so I there is a so temporary variable of this comprehension, an expression and you want
to say for I in content. So we are applying the rstrip method to the I variable
where I is the item of the content list so what this will do it will go through
the first string and it will strip out the backslash n from that first string
and then it will store it as first item of the new list and then it will go
to the second line and it will do the same until the list is consumed so execute that
and content and now we have a clean list. And yeah, that's what you should
know about opening and reading text out of text files with Python, but there's one
last crucial thing you should know, you know we still have this file opened in Python.
In this particular case we're opening that file in read mode,
but if you were writing something in that file and then you close your terminal
without closing the file, those changes would not be saved in the file, so you
should always apply the close method to your file object just like that and now
you can go ahead and delete the file if you like, so that was successful.
Again your variables are still there so once you read the content of the file and you
store that in a variable, then you can reuse your variables as many times as
you want and yeah, that's about opening and reading files with Python. In the next
lecture we'll go ahead and write content in the file, so I'll see you there.