1
00:00:01,540 --> 00:00:04,090
Hey, welcome to today's bonus example.

2
00:00:05,320 --> 00:00:16,270
What you see on my screen are two lists, a list stored in the contents variable, and it has three

3
00:00:16,270 --> 00:00:17,080
items.

4
00:00:20,300 --> 00:00:22,700
It's too long, but we're going to fix that.

5
00:00:27,570 --> 00:00:28,650
You'll see how.

6
00:00:29,100 --> 00:00:32,250
And then we have another list file names.

7
00:00:32,400 --> 00:00:34,410
This also has three items.

8
00:00:37,450 --> 00:00:40,480
So these are supposed to be five paths.

9
00:00:40,810 --> 00:00:42,340
Each of them is a string.

10
00:00:44,290 --> 00:00:49,420
And those are supposed to be the content that we'll give to each of these files.

11
00:00:50,020 --> 00:00:54,550
So the first content, the second content and the third content.

12
00:00:56,500 --> 00:01:05,170
So we're going to write a program, and when we execute that program, the program is going to generate

13
00:01:05,980 --> 00:01:09,760
these three files inside this files directory.

14
00:01:10,240 --> 00:01:13,510
And each of the files is going to contain.

15
00:01:15,160 --> 00:01:17,080
The corresponding content.

16
00:01:17,080 --> 00:01:21,400
So the first file is going to contain that first content.

17
00:01:21,430 --> 00:01:27,070
The second file is going to contain that second content and the third file is going to contain the third

18
00:01:27,070 --> 00:01:27,910
content.

19
00:01:28,390 --> 00:01:34,030
So let's see how we can create this program which generates these three files.

20
00:01:36,350 --> 00:01:43,190
Before we begin, I want you to create a python file and write this content.

21
00:01:43,550 --> 00:01:46,020
So two lists, three items each.

22
00:01:46,040 --> 00:01:49,910
You can write some dummy content, whatever you like for these strings.

23
00:01:52,980 --> 00:02:01,680
And I would also want you to create a file directory under the main project directory and just leave

24
00:02:01,680 --> 00:02:02,550
it empty.

25
00:02:03,820 --> 00:02:07,840
The directory will be populated by Python with these three files.

26
00:02:09,270 --> 00:02:12,780
The python file itself can be anywhere.

27
00:02:13,050 --> 00:02:15,510
It can be in the main directory.

28
00:02:16,880 --> 00:02:24,200
Where main that point is, or it can be in another sub directory, as in my case, it is here on the

29
00:02:24,210 --> 00:02:25,100
bonus.

30
00:02:25,100 --> 00:02:26,180
Bonus six.

31
00:02:27,170 --> 00:02:29,060
With that, let's begin coding.

32
00:02:30,880 --> 00:02:33,580
So how do we create multiple files?

33
00:02:34,000 --> 00:02:44,200
Well, when we say the word multiple, that means we need a full loop which iterates over content.

34
00:02:47,300 --> 00:02:48,920
And over file names.

35
00:02:49,040 --> 00:02:52,520
So we're looking at two variables at the same time.

36
00:02:52,520 --> 00:02:53,930
We need to.

37
00:02:55,430 --> 00:02:59,720
Fetch this, and that's in the first iteration.

38
00:02:59,810 --> 00:03:02,930
Then in the second iteration, fetch that.

39
00:03:02,930 --> 00:03:03,890
So get that.

40
00:03:03,890 --> 00:03:04,820
And this one.

41
00:03:05,300 --> 00:03:11,060
And to be able to do that, we use these two variables and we iterate over.

42
00:03:11,630 --> 00:03:13,910
Well, should we use enumerate?

43
00:03:15,770 --> 00:03:20,360
It doesn't sound like the solution to me because enumerates creates.

44
00:03:24,780 --> 00:03:26,970
These kinds of objects.

45
00:03:26,970 --> 00:03:28,650
So a multiple.

46
00:03:30,890 --> 00:03:33,170
To pulse like that.

47
00:03:33,200 --> 00:03:39,530
And each couple has the index as the first item and the item itself as the second item.

48
00:03:39,530 --> 00:03:43,280
But this time we're not interested about indices.

49
00:03:46,040 --> 00:03:47,210
We're interested.

50
00:03:48,680 --> 00:03:51,080
On the items of that and that.

51
00:03:51,080 --> 00:04:00,920
And to do that, we use zip the zip function, which works basically like enumerate, and I'll demonstrate

52
00:04:00,920 --> 00:04:01,580
you how.

53
00:04:03,830 --> 00:04:05,750
So if we have lists.

54
00:04:09,570 --> 00:04:15,240
A and list B, 1020, 30.

55
00:04:17,579 --> 00:04:22,890
Then X is going to be zip A and B.

56
00:04:23,460 --> 00:04:25,590
So that's what we enter here as well.

57
00:04:25,620 --> 00:04:26,370
Zip.

58
00:04:29,790 --> 00:04:33,360
Contents and final names.

59
00:04:34,680 --> 00:04:39,030
So not content and file name, but contents and file names.

60
00:04:39,030 --> 00:04:43,020
These two lists here as well.

61
00:04:43,020 --> 00:04:44,700
We have these two lists.

62
00:04:45,240 --> 00:04:49,110
So if you print out zip, you get this representation of the object.

63
00:04:49,110 --> 00:04:55,050
But if you convert it into list, as we did with the enumerate object, then you get this.

64
00:04:56,890 --> 00:05:02,290
Representation, which is the same structure as the structure of the enumerate object.

65
00:05:02,320 --> 00:05:08,770
The difference is that the first items here are actual items of the first list, and the second items

66
00:05:08,770 --> 00:05:11,200
are the items of the second list.

67
00:05:14,430 --> 00:05:19,410
So then we can continue our full loop here.

68
00:05:20,700 --> 00:05:27,720
We create a file object in each iteration and use the open function.

69
00:05:27,930 --> 00:05:29,350
So the file path.

70
00:05:29,370 --> 00:05:32,250
Now, what is file path going to be?

71
00:05:33,700 --> 00:05:35,980
The five paths are in five names.

72
00:05:35,980 --> 00:05:42,970
So we're going to use file name, file name extracts doc txt in the first iteration of both.

73
00:05:42,970 --> 00:05:48,580
In my case, the files are going to be inside files.

74
00:05:49,390 --> 00:05:53,650
Therefore, I want to construct a string.

75
00:05:54,970 --> 00:05:56,800
Using an F string.

76
00:05:58,300 --> 00:06:03,460
So it's going to be a file slash, and then file name is the variable.

77
00:06:07,550 --> 00:06:09,290
And then close the string.

78
00:06:10,580 --> 00:06:24,200
So what we're doing here is if file name is equal to the text, then this is going to create the file

79
00:06:24,200 --> 00:06:27,590
path files slash dog dot text.

80
00:06:29,990 --> 00:06:33,530
So a file in this path is going to be created

81
00:06:35,900 --> 00:06:38,060
in right mode.

82
00:06:38,060 --> 00:06:40,030
So we want to create files.

83
00:06:40,040 --> 00:06:43,760
That means we want to say right here, not read.

84
00:06:47,550 --> 00:06:49,260
And then file that right.

85
00:06:51,480 --> 00:06:53,100
And here goes the content.

86
00:06:54,570 --> 00:06:58,500
So in the first iteration, content will be this first string.

87
00:07:00,150 --> 00:07:02,810
Let's try to run this run.

88
00:07:02,820 --> 00:07:09,480
We get an error initially file not found, not such file or directory file slash total text.

89
00:07:12,640 --> 00:07:15,610
How comes when files actually exist?

90
00:07:15,880 --> 00:07:18,220
Even if I delete this files.

91
00:07:20,720 --> 00:07:23,670
Because, of course, we expect the program to create them.

92
00:07:23,690 --> 00:07:26,540
So if I run this again, we're going to get the same error.

93
00:07:26,540 --> 00:07:30,770
So it's not related to the files being there already.

94
00:07:31,220 --> 00:07:34,460
The problem is that my bonus six dot.

95
00:07:35,910 --> 00:07:39,750
P file is inside this bonus directory.

96
00:07:40,110 --> 00:07:43,560
So bonuses here and files are there.

97
00:07:44,610 --> 00:07:51,260
So what we need to do is we first want to go one directory up using these two dots.

98
00:07:51,270 --> 00:07:54,570
That's the syntax and then slash.

99
00:07:55,290 --> 00:07:58,560
So that means the python file is.

100
00:07:59,410 --> 00:08:02,140
Inside a bonus folder.

101
00:08:02,140 --> 00:08:04,390
So we go up one level.

102
00:08:05,560 --> 00:08:11,950
And at this point we are in the app one directory, which is one level up bonus.

103
00:08:12,310 --> 00:08:14,290
And then we go to files.

104
00:08:16,390 --> 00:08:19,150
So at this point we can access files.

105
00:08:19,690 --> 00:08:27,940
If your script, if your bonus $0.06 file is in the root directory, then all you need to do is that

106
00:08:28,510 --> 00:08:30,160
you don't need to write this part.

107
00:08:31,270 --> 00:08:36,250
So if I run this now, we're going to get the files generated successfully.

108
00:08:36,490 --> 00:08:37,870
Doctor text.

109
00:08:37,900 --> 00:08:42,130
Presentation The text and reports the text.

110
00:08:52,070 --> 00:08:55,430
Now, I mentioned earlier that.

111
00:08:56,290 --> 00:08:58,700
This list is too long.

112
00:08:58,720 --> 00:09:04,890
We can fix that by splitting these into multiple lines.

113
00:09:04,900 --> 00:09:10,900
All you have to do is go after the first comma and press.

114
00:09:13,280 --> 00:09:21,400
Enter and then go to the second comma press enter and the list will be spread out in multiple lines.

115
00:09:21,410 --> 00:09:22,430
It's still a list.

116
00:09:22,430 --> 00:09:24,470
The object is exactly the same.

117
00:09:26,080 --> 00:09:28,340
So this is also part of the syntax.

118
00:09:28,360 --> 00:09:31,990
Another way to create a list if another program.

119
00:09:32,350 --> 00:09:33,580
It will run again.

120
00:09:33,580 --> 00:09:36,020
So we still have these files.

121
00:09:36,040 --> 00:09:43,600
These are always overwritten whenever we run the program because the right method overrides any existing

122
00:09:43,600 --> 00:09:44,290
files.

123
00:09:46,200 --> 00:09:53,130
So that's how you can create multiple lines of code for one single expression.

124
00:09:53,130 --> 00:10:00,960
So the expression in this case is this list construction, and you can also split the string if you

125
00:10:00,960 --> 00:10:01,160
want.

126
00:10:01,170 --> 00:10:06,780
So if the string was very long and maybe you want to split this here, you can just put your cursor

127
00:10:06,780 --> 00:10:10,500
there, press enter and Char will split it.

128
00:10:10,530 --> 00:10:15,810
If you are on another ID, then maybe this will not work automatically.

129
00:10:15,810 --> 00:10:18,780
So perhaps you you want to do this manually.

130
00:10:18,960 --> 00:10:24,480
So the way you split the string is you just close the first string where you want it.

131
00:10:24,750 --> 00:10:25,620
And then.

132
00:10:26,600 --> 00:10:28,040
Close the quote.

133
00:10:28,640 --> 00:10:34,160
So you open quotes here, you open quote there, and then in the next line again, you open the quotes

134
00:10:34,160 --> 00:10:35,610
and you close the quote.

135
00:10:35,760 --> 00:10:39,380
So so all of that is treated as one string.

136
00:10:39,380 --> 00:10:41,540
So it is one single string.

137
00:10:42,800 --> 00:10:46,460
Note that this will not work in the Python console.

138
00:10:46,550 --> 00:10:52,940
So if you want to have a string in multiple lines and you execute, that string is actually going to

139
00:10:52,940 --> 00:10:53,840
be executed.

140
00:10:54,080 --> 00:10:59,330
It is going to be stored in the Python current session.

141
00:11:01,140 --> 00:11:06,270
So this split only works when you are working on that P Files.

142
00:11:09,220 --> 00:11:16,310
Now, if the string was not in a list, it would have to be split differently.

143
00:11:16,330 --> 00:11:26,440
So let's suppose that A is one single string I must string on my own.

144
00:11:31,000 --> 00:11:36,910
So when strings are individual, you can still press, enter there and split it.

145
00:11:38,730 --> 00:11:43,520
A birds python pie charm actually will add this backslash.

146
00:11:43,530 --> 00:11:46,590
And that means this is still one string.

147
00:11:47,250 --> 00:11:53,120
So if the string is outside of the list, a backslash is used.

148
00:11:53,130 --> 00:11:57,180
Additionally, both parts of the string are still.

149
00:11:58,340 --> 00:12:00,650
Enclosed in quotes both.

150
00:12:00,650 --> 00:12:06,560
There is a backslash in the first line, and if you want to keep splitting, you want to add another

151
00:12:06,560 --> 00:12:10,220
backslash and the other part of the string there and so on.

152
00:12:10,220 --> 00:12:20,600
So this is the case when you have very long strings that go beyond the horizontal space of your editor.

153
00:12:23,330 --> 00:12:26,870
And it becomes useful to split them like that.

154
00:12:28,100 --> 00:12:33,110
So the code remains easier to to see, to read and manage.

155
00:12:34,750 --> 00:12:36,600
And that's about this example.

156
00:12:36,630 --> 00:12:37,950
Thanks a lot for following.

