[No audio]
So this is how you open a text file with
your favorite text editor program, and
let me show you how to do the same with
Python. Of course, experience is a
bit different. It's not about double
clicking the file, it's about writing
code. So when you open that file with
your text editor, what happens is that
some object is created in your temporary
memory of your computer, in RAM. So the
file was on the hard disk. When you open
it, it's created as some type of objects
in the RAM. Same thing happens with
Python. In this case, you might want to
store that object, that file object that
that you're going to create in Python, you
might want to store it in a variable.
Let's call it myfile.
The open function
is what creates file objects, and since
it is a function, it will get an
argument. Now, this basics.py file is in
the same directory with the fruits.txt file,
please download the fruits.txt file,
which looks like this,
or let me place it here, you can drag a file on
the side here to have two windows on
Visual Studio Code Editor, and you can
do that even in other editors, I'm sure,
if you're using other editors. So this
file for now, let it be in the same
folder with basics.py. Once you do
that, then the input for this function
is the path to the file. In this case,
that will be fruits.txt. Since the
fruits.txt file is in the
same directory with basics.py. Now to
read the contents of fruits.txt in
your text editor like we did
graphically, it's just about double
clicking the file and then you see the
content on the Text Editor interface. In
Python, things are a bit more, they have
to be a bit more explicit, you have to
specify what you want to do. Because
this file, we have just created a file
object. If we execute that, nothing is
going to happen. An object is going to
be created in temporary memory, and when
the execution of the program ends, the
object will be deleted from the memory,
just like when you open the text
file with the text editor, and then you
close it, the object, the file object is
deleted from RAM, from the temporary memory.
So if you want to read, what you might
want to do is print out myfile. Now myfile
has a read method. So this is the
object, the file object which has a read
method. It doesn't take any arguments.
So just leave it like that, Save,
execute, and here you get the output of
the file. This is how you read a file in Python.
[No audio]