WEBVTT

00:00.080 --> 00:01.000
Welcome back.

00:01.040 --> 00:02.760
We learned about try and catch.

00:02.800 --> 00:05.960
Now let's learn about file input output.

00:06.000 --> 00:12.000
You can read from and write to files on your computer using Python.

00:12.280 --> 00:13.760
Common use cases.

00:13.800 --> 00:17.720
Save the user data log events and load configuration.

00:17.760 --> 00:24.800
And by the way we're going to use this file input output in the next videos in order to convert between

00:24.800 --> 00:27.600
models and train data.

00:27.640 --> 00:31.840
Here let me start with writing to a file.

00:31.880 --> 00:34.800
Try open file in writing mode.

00:35.080 --> 00:37.520
Open Myfile.txt.

00:37.760 --> 00:40.760
Right, right and then close.

00:40.880 --> 00:44.520
And this is a try exception format.

00:44.560 --> 00:51.400
Let me run data written to my text Myfile.txt successfully.

00:51.640 --> 00:58.120
If we open here this folder a new file is being created.

00:58.120 --> 00:59.840
Let me double click on it.

00:59.960 --> 01:00.760
And here we go.

01:00.800 --> 01:04.660
This is a text file we wrote on it.

01:04.700 --> 01:06.580
Hello from Python.

01:06.620 --> 01:08.460
This is line two.

01:08.500 --> 01:13.740
You can download, by the way, this to your, uh, to your computer.

01:13.740 --> 01:15.740
So let me open it.

01:15.980 --> 01:19.020
It's a text file containing two lines.

01:19.020 --> 01:20.500
Hello from Python.

01:20.660 --> 01:22.140
This is line two.

01:22.300 --> 01:29.340
So this is how we handle the file input output and how to write to a file.

01:29.340 --> 01:37.180
So let me show you again guys inside this try block we have a file variable.

01:37.540 --> 01:40.300
Uh we are calling the open function.

01:40.500 --> 01:47.780
And we specify the name of the file and w and writing mode file dot.

01:47.780 --> 01:48.500
Right.

01:48.540 --> 01:52.380
We are writing to the file hello from Python.

01:52.660 --> 01:57.460
Then we are going to write another line which is line two.

01:57.780 --> 01:58.940
This is line two.

01:59.140 --> 02:07.680
And then file.close always close the file in order to prevent any errors and print data returned to

02:07.720 --> 02:08.600
my file.

02:08.640 --> 02:10.240
Text successfully.

02:10.520 --> 02:14.680
This is the message and this is the file being created.

02:14.720 --> 02:17.680
Double click on it and this is our file.

02:17.920 --> 02:18.400
Okay.

02:18.600 --> 02:22.280
Now let me try to read from a file.

02:22.600 --> 02:27.560
To open a file in reading mode you specify the file equals to open.

02:27.600 --> 02:30.240
And similar to what we've done before.

02:30.440 --> 02:37.520
But this time we use R which is the reading mode and not w w write mode.

02:37.560 --> 02:40.760
Here we are opening it for reading mode.

02:40.960 --> 02:43.960
Content equals to file dot read.

02:44.080 --> 02:51.280
I'm reading the file input stream and file dot close contents of my file.

02:51.480 --> 02:59.480
Since I, I've, uh, read the file and stored it inside a variable called content, I can print the

02:59.480 --> 03:01.000
content of this file.

03:01.040 --> 03:05.480
Otherwise, if I didn't find, for example, myfile.txt file.

03:05.740 --> 03:14.020
if there is no file like this, if uh for any, for any reason, we can't, we can't read from this

03:14.020 --> 03:14.540
file.

03:14.580 --> 03:18.060
Go and execute this exception error reading file.

03:18.100 --> 03:24.380
Let me run again contents of my file dot txt hello from Python.

03:24.420 --> 03:25.740
This is line two.

03:25.940 --> 03:33.620
Let me show you guys if I, uh, like, uh, modify this, uh, this file for example.

03:33.780 --> 03:39.020
Hello again from Master Coding.

03:39.140 --> 03:48.460
Please rate us five stars on Udemy to support us and visit Master coding.io.

03:48.620 --> 03:50.460
This is our official website.

03:50.460 --> 03:54.620
We are developing it and soon we're going to release it.

03:54.660 --> 03:59.220
Okay so here let me save the file.

03:59.260 --> 04:02.500
Double click again and it's saved being saved.

04:02.740 --> 04:07.360
Now let me read the file run again and here we go.

04:07.400 --> 04:10.040
Contents of my file.txt.

04:10.080 --> 04:11.200
Hello from Python.

04:11.200 --> 04:12.200
This is line two.

04:12.320 --> 04:16.320
And those are the content, the new content of the file.

04:16.320 --> 04:19.880
So we are reading this file directly.

04:20.120 --> 04:21.000
Okay guys.

04:21.000 --> 04:28.640
So in this video we learned how to create a file how to write data to the file and then close it.

04:28.640 --> 04:33.480
Also we're going we learned how to read from a file.

04:33.520 --> 04:34.840
We open the file.

04:35.000 --> 04:40.280
We store the content of the file inside a content variable and close it.

04:40.280 --> 04:43.000
Then we print the content of this file.

04:43.040 --> 04:45.520
Use with statement.

04:45.720 --> 04:48.960
It automatically closes the file.

04:48.960 --> 04:55.960
So here with writing with with with open as file go and file.

04:56.480 --> 04:59.720
Write to the file appending line edit.

04:59.760 --> 05:04.040
It automatically closes the file in order to prevent any errors.

05:04.080 --> 05:09.400
Okay, this is how we write and read files in Python.
