Welcome back.
We learned about try and catch.
Now let's learn about file input output.
You can read from and write to files on your computer using Python.
Common use cases.
Save the user data log events and load configuration.
And by the way we're going to use this file input output in the next videos in order to convert between
models and train data.
Here let me start with writing to a file.
Try open file in writing mode.
Open Myfile.txt.
Right, right and then close.
And this is a try exception format.
Let me run data written to my text Myfile.txt successfully.
If we open here this folder a new file is being created.
Let me double click on it.
And here we go.
This is a text file we wrote on it.
Hello from Python.
This is line two.
You can download, by the way, this to your, uh, to your computer.
So let me open it.
It's a text file containing two lines.
Hello from Python.
This is line two.
So this is how we handle the file input output and how to write to a file.
So let me show you again guys inside this try block we have a file variable.
Uh we are calling the open function.
And we specify the name of the file and w and writing mode file dot.
Right.
We are writing to the file hello from Python.
Then we are going to write another line which is line two.
This is line two.
And then file.close always close the file in order to prevent any errors and print data returned to
my file.
Text successfully.
This is the message and this is the file being created.
Double click on it and this is our file.
Okay.
Now let me try to read from a file.
To open a file in reading mode you specify the file equals to open.
And similar to what we've done before.
But this time we use R which is the reading mode and not w w write mode.
Here we are opening it for reading mode.
Content equals to file dot read.
I'm reading the file input stream and file dot close contents of my file.
Since I, I've, uh, read the file and stored it inside a variable called content, I can print the
content of this file.
Otherwise, if I didn't find, for example, myfile.txt file.
if there is no file like this, if uh for any, for any reason, we can't, we can't read from this
file.
Go and execute this exception error reading file.
Let me run again contents of my file dot txt hello from Python.
This is line two.
Let me show you guys if I, uh, like, uh, modify this, uh, this file for example.
Hello again from Master Coding.
Please rate us five stars on Udemy to support us and visit Master coding.io.
This is our official website.
We are developing it and soon we're going to release it.
Okay so here let me save the file.
Double click again and it's saved being saved.
Now let me read the file run again and here we go.
Contents of my file.txt.
Hello from Python.
This is line two.
And those are the content, the new content of the file.
So we are reading this file directly.
Okay guys.
So in this video we learned how to create a file how to write data to the file and then close it.
Also we're going we learned how to read from a file.
We open the file.
We store the content of the file inside a content variable and close it.
Then we print the content of this file.
Use with statement.
It automatically closes the file.
So here with writing with with with open as file go and file.
Write to the file appending line edit.
It automatically closes the file in order to prevent any errors.
Okay, this is how we write and read files in Python.