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 first created a new Python file here, read-write.python for this section.
And then in the same directory here, at the same level, I have created a file test.
Okay, this is just the name for the file, which contains a list of numbers.
And actually, you can see we have just one number per line.
Okay, those are integer numbers with positive and negatives.
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 going to 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 are 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, all from the file manager.
And now, how we are 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 or the path.
The second is R for reading, and then you have as F colon.
Go back to a new line with indentation.
And if you want, for example, to print the entire
file, you can do print, and then F dot read.
So let's actually run that.
And you can see we have the content 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 with file 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 ask 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.
Okay, 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.
All right, 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, I'm going to remove 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.
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 new line character for each line you read.
And to do that, you simply do line.rsstrip.
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 new line character for each line.
And the r.sstrip 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 on new line 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 or 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, so you 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 see users, my username, and then PyCharm
projects, my first project and the file name.
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 Mac OS, 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, and 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 a recognized
backslash F, which is going to do something.
If I put a backslash here, it's simply a normal backslash, okay?
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 width 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 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.