1
00:00:00,360 --> 00:00:01,220
Hey, welcome back.

2
00:00:01,230 --> 00:00:08,189
Let's try some cold experiments, try to test things out so that you understand some certain aspects

3
00:00:08,189 --> 00:00:08,760
better.

4
00:00:08,760 --> 00:00:16,050
So we're working on the wave context manager today, and I want you to create a new file and write this

5
00:00:16,050 --> 00:00:16,770
code there.

6
00:00:18,640 --> 00:00:19,810
This code.

7
00:00:20,770 --> 00:00:22,930
Tries to open this file.

8
00:00:23,170 --> 00:00:26,260
The problem is this file does not exist here.

9
00:00:26,440 --> 00:00:30,710
There is a file folder as you see here.

10
00:00:30,730 --> 00:00:31,360
Files.

11
00:00:31,360 --> 00:00:32,790
Files are both.

12
00:00:32,800 --> 00:00:33,820
There is no docs.

13
00:00:34,060 --> 00:00:34,960
TXT file.

14
00:00:34,990 --> 00:00:38,980
There is a doc the TRC file, but there is no docs that takes file.

15
00:00:39,010 --> 00:00:42,490
Therefore, whenever I run the script I'm going to get an error.

16
00:00:45,880 --> 00:00:48,460
The error says that the file was not found.

17
00:00:49,330 --> 00:00:55,090
And of course that can be fixed by pointing to the right file path.

18
00:00:55,120 --> 00:00:59,010
In my case it is file slash doc text.

19
00:00:59,170 --> 00:01:00,790
You see, I have this file here.

20
00:01:01,120 --> 00:01:06,130
So if you want the code to to work, you need that file to exist.

21
00:01:10,020 --> 00:01:14,460
In my case, I also need to add these dots in front.

22
00:01:15,660 --> 00:01:17,370
As I explained earlier.

23
00:01:19,530 --> 00:01:27,090
These dots are useful when the python file is somewhere in the directory.

24
00:01:29,720 --> 00:01:33,470
And the external text file is outside of the directory.

25
00:01:33,480 --> 00:01:43,010
So in my case, my python file is inside the experiments and the dog, the text file is in files here.

26
00:01:44,870 --> 00:01:47,420
This time the code will work fine.

27
00:01:49,730 --> 00:01:50,230
There we go.

28
00:01:50,240 --> 00:01:51,170
We didn't get any.

29
00:01:52,400 --> 00:01:54,860
Output because we didn't print this out.

30
00:01:57,460 --> 00:02:04,750
So that was the first thing I wanted to show you that file paths need to exist when you open a file

31
00:02:04,750 --> 00:02:05,980
in read mode.

32
00:02:08,520 --> 00:02:15,720
The next thing I want you to try is I want you to remove this argument, remove it, and keep only the

33
00:02:15,720 --> 00:02:16,830
first argument.

34
00:02:19,010 --> 00:02:20,330
And run the code.

35
00:02:22,770 --> 00:02:24,720
So there are no errors.

36
00:02:25,050 --> 00:02:32,310
And of course, you can print out the content of the file and you get the content successfully.

37
00:02:32,310 --> 00:02:35,670
So all carrots are to be sliced longitudinally.

38
00:02:35,970 --> 00:02:38,190
That's the contents of this file.

39
00:02:41,880 --> 00:02:43,990
So why is it still working?

40
00:02:44,010 --> 00:02:48,150
Well, that's because by default, the open function.

41
00:02:49,240 --> 00:02:55,810
Assigns an r value to the modes argument so you can open a python console.

42
00:02:59,310 --> 00:03:01,590
And check the help for open.

43
00:03:03,390 --> 00:03:04,680
Scroll up.

44
00:03:05,790 --> 00:03:06,870
To the top.

45
00:03:10,080 --> 00:03:14,940
And you'll see that the mode argument is by default set to our.

46
00:03:16,720 --> 00:03:21,550
So you'll see some programmers just use this version when they want to read.

47
00:03:23,260 --> 00:03:24,130
A file.

48
00:03:27,500 --> 00:03:31,970
And then if they want to write, they use this version with a right argument.

49
00:03:31,970 --> 00:03:42,410
So when you want to write a file, then you have to use W with or it's up to your personal preference.

50
00:03:43,890 --> 00:03:47,820
So not mentioning the arguments makes the call shorter.

51
00:03:48,870 --> 00:03:55,770
But some would argue that having the argument value there is better because it makes the codes more

52
00:03:55,770 --> 00:03:56,400
explicit.

53
00:03:56,400 --> 00:03:58,350
So you can read the code better.

54
00:03:58,350 --> 00:04:00,840
As a programmer for Python is the same.

55
00:04:03,330 --> 00:04:04,650
The next experiment.

56
00:04:04,680 --> 00:04:08,250
Let's try to print out here anything like Hello.

57
00:04:09,990 --> 00:04:10,890
Run the codes.

58
00:04:11,870 --> 00:04:17,690
You get sell printed out, obviously, because the with contacts manager first opens the file and then

59
00:04:17,690 --> 00:04:20,180
it executes whatever it finds in here.

60
00:04:21,980 --> 00:04:29,570
So the with context manager doesn't actually force you to have a file release methods here.

61
00:04:30,640 --> 00:04:32,890
But of course you do want to have that.

62
00:04:32,890 --> 00:04:40,090
So but my point here is that if you decide to have file read outside with context manager.

63
00:04:40,300 --> 00:04:46,300
So you see unintended here, this is not inside the width block anymore.

64
00:04:46,300 --> 00:04:49,210
So if I run the code now, we're going to get an error.

65
00:04:51,450 --> 00:04:53,130
It points to line three.

66
00:04:53,130 --> 00:04:56,970
You can click there, takes you to line three in the right methods.

67
00:04:56,970 --> 00:04:58,080
That's a problem.

68
00:04:58,080 --> 00:05:04,980
So we're operating on a closed file because as I told you, the with contacts manager opens the file

69
00:05:04,980 --> 00:05:15,330
and then once the block of code ends execution, so once the indented code block ends execution, where

70
00:05:15,330 --> 00:05:17,610
the file is closed implicitly.

71
00:05:18,830 --> 00:05:23,690
An interpreter goes on and executes the other lines.

72
00:05:25,510 --> 00:05:27,910
Now notice that this is not.

73
00:05:28,760 --> 00:05:32,600
A problem with a variable, so the variable still exists.

74
00:05:32,600 --> 00:05:35,360
File is a variable and it does exist.

75
00:05:36,620 --> 00:05:43,730
But that variable at this point is associated with a closed file up to here.

76
00:05:43,730 --> 00:05:49,850
It is associated with an open file, but now the file is closed, so you cannot read a closed file.

77
00:05:50,060 --> 00:05:51,200
That's the idea here.

78
00:05:53,230 --> 00:05:59,080
So to fix this, of course you need to cut that and put it inside the width block.

79
00:06:00,010 --> 00:06:06,970
And lastly, the last thing I want to destroy is that you already know this, but I wanted to emphasize

80
00:06:06,970 --> 00:06:11,650
this, that once you assign the variable to the file, read.

81
00:06:13,030 --> 00:06:20,170
Methods, then you can use the variable as many times as you want.

82
00:06:22,970 --> 00:06:28,880
So you run the scripts and the variable will be printed out as many times as there are print functions

83
00:06:28,880 --> 00:06:29,540
in here.

84
00:06:33,350 --> 00:06:40,940
This wouldn't be the case, though, if you had to file that read methods, as I explained earlier.

85
00:06:40,940 --> 00:06:47,030
So I also wanted to mention this again, be careful with this because some many people are just confused

86
00:06:47,030 --> 00:06:52,940
and they cannot find out why the program doesn't behave as they expected.

87
00:06:52,940 --> 00:06:55,300
So in this case, we don't get any output.

88
00:06:55,310 --> 00:07:05,750
That's because the string is first created in here by the read method and then the read method is exhausted.

89
00:07:06,890 --> 00:07:13,220
So the second time it returns an empty screen, because as I explained the first time the right method

90
00:07:13,520 --> 00:07:19,100
reads the string up to here, the content of the text file, it reads it up to here.

91
00:07:19,100 --> 00:07:26,690
And then if you execute the read method again, it tries to read whatever is at the end here.

92
00:07:26,690 --> 00:07:27,930
So there is nothing always.

93
00:07:27,930 --> 00:07:32,120
So that's why you get an empty string.

94
00:07:33,080 --> 00:07:38,960
So the second time this outputs an empty string and the empty string is stored in here.

95
00:07:38,960 --> 00:07:42,500
So we are printing out an empty string each time.

96
00:07:42,500 --> 00:07:44,570
That's why we get nothing here.

97
00:07:45,860 --> 00:07:47,800
That's about the experiments for the day.

98
00:07:47,810 --> 00:07:48,470
Thanks a lot.

