Hey, welcome back.
Let's try some cold experiments, try to test things out so that you understand some certain aspects
better.
So we're working on the wave context manager today, and I want you to create a new file and write this
code there.
This code.
Tries to open this file.
The problem is this file does not exist here.
There is a file folder as you see here.
Files.
Files are both.
There is no docs.
TXT file.
There is a doc the TRC file, but there is no docs that takes file.
Therefore, whenever I run the script I'm going to get an error.
The error says that the file was not found.
And of course that can be fixed by pointing to the right file path.
In my case it is file slash doc text.
You see, I have this file here.
So if you want the code to to work, you need that file to exist.
In my case, I also need to add these dots in front.
As I explained earlier.
These dots are useful when the python file is somewhere in the directory.
And the external text file is outside of the directory.
So in my case, my python file is inside the experiments and the dog, the text file is in files here.
This time the code will work fine.
There we go.
We didn't get any.
Output because we didn't print this out.
So that was the first thing I wanted to show you that file paths need to exist when you open a file
in read mode.
The next thing I want you to try is I want you to remove this argument, remove it, and keep only the
first argument.
And run the code.
So there are no errors.
And of course, you can print out the content of the file and you get the content successfully.
So all carrots are to be sliced longitudinally.
That's the contents of this file.
So why is it still working?
Well, that's because by default, the open function.
Assigns an r value to the modes argument so you can open a python console.
And check the help for open.
Scroll up.
To the top.
And you'll see that the mode argument is by default set to our.
So you'll see some programmers just use this version when they want to read.
A file.
And then if they want to write, they use this version with a right argument.
So when you want to write a file, then you have to use W with or it's up to your personal preference.
So not mentioning the arguments makes the call shorter.
But some would argue that having the argument value there is better because it makes the codes more
explicit.
So you can read the code better.
As a programmer for Python is the same.
The next experiment.
Let's try to print out here anything like Hello.
Run the codes.
You get sell printed out, obviously, because the with contacts manager first opens the file and then
it executes whatever it finds in here.
So the with context manager doesn't actually force you to have a file release methods here.
But of course you do want to have that.
So but my point here is that if you decide to have file read outside with context manager.
So you see unintended here, this is not inside the width block anymore.
So if I run the code now, we're going to get an error.
It points to line three.
You can click there, takes you to line three in the right methods.
That's a problem.
So we're operating on a closed file because as I told you, the with contacts manager opens the file
and then once the block of code ends execution, so once the indented code block ends execution, where
the file is closed implicitly.
An interpreter goes on and executes the other lines.
Now notice that this is not.
A problem with a variable, so the variable still exists.
File is a variable and it does exist.
But that variable at this point is associated with a closed file up to here.
It is associated with an open file, but now the file is closed, so you cannot read a closed file.
That's the idea here.
So to fix this, of course you need to cut that and put it inside the width block.
And lastly, the last thing I want to destroy is that you already know this, but I wanted to emphasize
this, that once you assign the variable to the file, read.
Methods, then you can use the variable as many times as you want.
So you run the scripts and the variable will be printed out as many times as there are print functions
in here.
This wouldn't be the case, though, if you had to file that read methods, as I explained earlier.
So I also wanted to mention this again, be careful with this because some many people are just confused
and they cannot find out why the program doesn't behave as they expected.
So in this case, we don't get any output.
That's because the string is first created in here by the read method and then the read method is exhausted.
So the second time it returns an empty screen, because as I explained the first time the right method
reads the string up to here, the content of the text file, it reads it up to here.
And then if you execute the read method again, it tries to read whatever is at the end here.
So there is nothing always.
So that's why you get an empty string.
So the second time this outputs an empty string and the empty string is stored in here.
So we are printing out an empty string each time.
That's why we get nothing here.
That's about the experiments for the day.
Thanks a lot.