In this lesson, I'm going to show you how you can
read some text from a file with Python and how to
organize this text so you can use it in your
program. So I have just created a new Python file
here read_write.py for this section. And
then in the same directory here, and the same
level, I have created the file_test, okay, this is
just the name for the file, which contains the
list of numbers, and actually, you can see we have
just one number per line, okay. Those are integer
numbers with positive and negative. One thing you
can see is that this file doesn't have any
extension, okay. You can create a file without any
extension, that's not the problem. On Windows,
it's gonna say that maybe it's not going to
recognize the file. But you actually don't need an
extension to create a text file. You're going to
be able to read it from Python without any
problem. So you can create a file, put it in the
same folder here, if you want to do that, you can
just create a New, File here, don't create a Python
file, just create a new file, give it a name. And
that's it , or from the File Manager. And now how
we're going to read that file from Python? So I'm
going first to write the code structure, okay, and
that's going to be the same every time. And then
I'm going to explain it. So with_ keyword, with, so
open function. The first parameter is the name of
the file, the path. The second is r for reading,
and then you have as f colon, go back to newline,
with indentation. And if you want, for example, to
print the entire file, you can print and then f.read.
So let's actually run that. And you can
see we have the contents of the file here. Okay,
all the numbers you see here are here. So that
worked. And now let me explain that a bit more. So
this structure, you are going to use it to read
from any file, okay. You start with the with
keyword, okay. And then open function, okay, the
thing we find is that you need to first open it,
and then you need to close the file. That's very
important. Here, you can see we don't close the
file, we don't call a close function. Why is that?
Because the with keyword is going to take care of
that. Okay. So whatever is after the with, you can
be sure that you can do whatever you want with
this file. And then when it's going to go out, or
if you have an error, the with keyword will make
sure that the file is closed, so you can open it
again safely the next time. So the function
open is going to open the file. You need first to
provide the path. Okay, so this is the path, this
is directly the name of the file because we are in
the same folder. And then you need to provide, so
between quotes, you need to provide the
permission, what do you want to do with the file.
Here, we just want to read. So you're going to put
the letter r, as simple as that. And then you have
as keyword, and then f, f is going to be the
variable you can use inside the with block, and
this represents actually the file that was opened
here. So you write this and then you can do
whatever you want with the file. And now we have f.read,
which is a function you can use on a
file, which is simply going to read the file. So
to read everything, and we just print that. And
now what if we want for example, to read each line
separately and not just get the whole text in one
block. What you can do something to run that you
can do for line in f. So this also is a very
common structure, for line in f, and let's do print
line. So for line in f, so the file that you get
here is going to iterate through all the lines of
the file. So I can run this and you can see we
still have the same numbers. But we also have a
new line every time. Okay, we have an additional
new line. Okay, so how to get rid of this. So this
is very common when you do this structure. How to
get rid of this? Well, line is a string, okay. And
the thing is that every time you go to a new line
with this structure, it's going to add a new line
character. So you need to remove that newline
character for each line you read. And to do that,
you simply do line.rstrip, okay. This is
a function on string, and you can do quote and
backslash n. So actually what is this backslash n?
Okay, if I am going to do something print, I'm
going to do A backslash n B backslash n C. Okay,
and I'm going to run that and you can see we have
A, B, C each on a new line. Okay, the backslash n
character is actually a special character. So
when you do backslash plus letter, you
usually have a special character. And this is
simply going back to a new line. So the problem
with this structure is simply that you get a new
newline character for each line, and the rstrip
is simply going to remove it from the end of the
string. So if you have this string, for example,
and you use that function, it's going to remove
the last backslash n. Okay, so now I'm going to
run it like this. And you can see, we have each
line without any additional backslash n or
newline character. Great. So now you can open the
file and process each line separately. What I'm
going to show you now is about this, I'm going to
come back to this file name of file path. So you
have two ways to provide the path here, either the
relative path, which is what we've done here. So
relative is simply that, where is this file
relative to that one, this is in the same folder
to just write the name of the file. If this is
somewhere else, you need to change the path. Okay.
So if you create a new folder, and you put this in
the folder, you need to put the name of the folder
first. Now, what you can also do is to provide the
absolute path. So I'm going to go here and right
click, and do Copy Path. Okay. And you can see, so
we have the relative path, which is that one Path
From Content Root, and we have the absolute path,
which is actually where the file is exactly. Okay.
So c:\Users\ my user name, and then PyCharmProjects\
my_first_project and the filename. So
I'm going to copy this, and I'm going to replace it here.
And using this, I'm going to be able to open the
file the same way. But for Windows, we need to
change something, okay, if you're using Linux or
macOS, you don't need to change anything on the
path because it's going to use forward slashes,
okay, so forward slash is simply like this, okay.
But on Windows, you have backslashes. And as you
have seen with the backslash n, when you use a
backslash, usually it means that you have a
special character. So what we need to do if you
want to use a backslash in a string, actually,
without triggering a special character, you need
to double each backslash. So that's what I'm going
to do. Every time I see a backslash, I double it
with another backslash. As you can see here, you
have the recognized backslash f, which is going to
do somethin. If I put a backslash here, it's
simply a normal, backslash, OK. And now I have the
absolute path, I'm going to run this and this is
going to work the same. So you can either provide
the absolute path or the relative path, depending
on what is more convenient. So now what you can do
with this, well, you can do whatever you want. For
example, you could create a number list again,
empty number list before the with structure. And
then for each line, you're going to add the new
line or the new number in the list. Okay, so make
sure you also cast and the number so cast this to
integer float, so you can make them usable, okay
in your list when you want to make computations
with numbers. All right, so now you can read from
a file, and you can also see how to organize the
content you read so you can then process it in
your Python code.