Let's talk about file I oh, what does I mean, you'll see this a lot in programming.
IO 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, let's say, an editor like sublime text that I have here or PAE
Charm or an online Reppel.
But usually you want to interact with different parts of the system.
Maybe you want to speak to another website.
Maybe you want to 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 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 IO 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 while
we need IO, then I need to input an image and then I need to update the compressed.
Maybe I want to 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 PDF.
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 dot text.
So if I do else here you see that I have a test text file and a script before.
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 heartstring test, dot text 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 Python three and then run script.
I get an object, a textile wrapper, I get the name, which is text's I get mowed, 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 eight.
Most of the files are usually encoded in UTF eight.
All right, so how can I actually read this file?
All we need to do is my file has a dot.
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 text, so let's write something.
I'll open this file and sublime text and just write, Hi, my name is Andre Agassi.
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 Andre, 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.
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 curser.
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 dot seke, 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 read line.
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 in here, if I now read the line.
I get hi, my name is Andre, if I read the line again, hi, my name is Andre 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 Andre 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 read lines 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 Andre Nagui.
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 and the program is just a good standard, so what you have to do is
say my file dot 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 fall 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.