Andrei: Let's talk about file I/O.
What does I/O mean?
You'll see this a lot in programming.
I/O stands for input output.
You see, most of the times, machines are not communicating
in just one environment.
For example, so far we've been writing our code in a,
let's say, an editor like Sublime Text that I have here,
or PyCharm or an online repo,
but usually, you wanna interact
with different parts of the system.
Maybe you wanna speak to another website.
Maybe you wanna speak to something
that's on your desktop maybe a file.
Maybe two different machines are communicating
with each other.
Maybe you're speaking to a database.
I/O simply means, "Hey,
I want you to input something from the outside world
and output something into the outside world."
And one of the most common ways that we use things
like I/O is through reading files.
You might think about Python and how wouldn't it be nice
if we can perhaps write a script that compresses images.
Well, we need I/O then, I need to input an image
and then I need to output the compressed image.
Maybe I wanna work with a PDF file
and maybe add a watermark to all my PDF pages.
Well, then we input a PDF and output a new version of a PDF.
So this is a very common task that we see a lot of,
and reading and writing files is a very important tool
in our tool belt.
And by the way, we have a project coming up
where we actually are going to use this with PDFs.
But for now,
how can we do this file input output with Python?
Well, Python has a built-in function
that allows us to open and write to files,
and it's simply called open, nice and easy.
So, using open, we can do something like this.
I can create a sample text file in our desktop,
so I'm going to use my terminal here.
So if we look at present working directory,
I'm on a Desktop.
If you're on Windows,
then you might have to do some different commands in here,
but by now you should be pretty familiar
that you can create a file if you wanted to.
Now, I can do this manually or in my terminal,
I can actually do, if you're on a Mac
you can just do touch, and then I'll say test.txt.
So if I do ls here, you see
that I have a test.txt file and a script.py file.
And again, just to double check, if I go to my desktop,
yep, I have these two right here.
Perfect.
Now, in here, we can simply say
in our script file
open,
and the name of the file, in our case it's test,
make sure it's string, test.txt, just like that.
Now, I can assign this to a variable, calling it my_file,
and now we have, well, the file object.
So let's check this out.
If I do here, print my_file,
let's see what happens.
I'm going to run my code,
so let's say python3, and then run script.py.
All right, I get an object, a TextIOWrapper.
I get the name, which is text.
I get mode, which I'm not sure what it is yet, we'll learn.
And then encodings, which is how this file is encoded,
which is UTF-8.
Most of the files are usually encoded in UTF-8.
All right, so how can I actually read this file?
All we need to do is my file has a .read method on it
so that if I run this now,
well, I get a blank piece of space
because there's nothing on this test.txt file,
so let's write something.
I'll open this file in Sublime Text
and just write, hi my name is Andrei Neagoie.
I got really, really creative with this one.
Good job.
All right, let's go back.
So now if I read this file, look at that,
I'm able to read, hi my name is Andrei,
and now, there you go, I've read my file.
Nice and easy.
Now, if I run this again,
let's just read this multiple times.
If I click Run,
hmm, I'm able to read the first time around,
but these two times I'm not reading anything.
Why is that?
Well, this open function has this idea of a cursor,
that is, you can only read the file once
and once you open, it returns a file object,
and the contents of the file you can read,
and the contents of the file are read with a cursor,
just like you see here,
one by one and printed onto the screen,
but by the end of this first reading,
the cursor is going to be at the end of the file.
So now when it tries to read, it's going to be end
of the file and nothing will be left there.
So, the way we get around this is to do something like this,
we simply say, my_file.seek,
which moves our cursor to whatever index we want,
in our case, seek zero.
So if I run this now, there you go.
And if I move the cursor back and I save and run this,
all right, that's a lot better now,
and this is just to demonstrate
that Python uses this idea of a cursor to read a file.
Now, another unique thing that I can do is to do readline,
so that if I run this, it reads the line,
but let's say our text file has different lines,
so, how are you?
Let's say a smiley face here.
If I now read the line,
again, hi my name is Andrei.
If I read the line again, hi my name is Andrei,
because I get each line, I only get the first line.
If I print this multiple times,
and I run this, all right, that's better.
I get, hi my name is Andrei, smiley face, how are you?
Again, the cursor keeps moving, right?
So I can just keep reading the lines.
Another thing that I can do is to just say readlines.
If I run this, I get a list that contains the entire file,
reads all the lines,
and you see here that I have, hi my name is Andrei Neagoie,
I have a new line here, a smiley face, a new line,
remember, this is escape sequencing, and then, how are you?
And no new line here, because there's no new line,
it's the end of the file.
So, these are extremely useful.
Maybe we can use regular expressions to search
for a piece of text in a file.
That's pretty useful right?
Now, the very last thing we need to do
and this is a little annoying,
so I'll show you how we don't need to do it in the future,
but you actually have to manually close the file
after you've opened it with open,
so you can use it somewhere else in the program.
It's just a good standard.
So, what you have to do is say my_file.close
after you're done with it.
You tell your computer, "Hey,
you need to stop whatever you're doing.
I'm not interested in the file anymore.
We're done with it.
You can use it somewhere else."
So, usually we do something like this and we're all done.
Let's take a break and learn some more in the next video.