Hey, welcome to today's bonus example.
What you see on my screen are two lists, a list stored in the contents variable, and it has three
items.
It's too long, but we're going to fix that.
You'll see how.
And then we have another list file names.
This also has three items.
So these are supposed to be five paths.
Each of them is a string.
And those are supposed to be the content that we'll give to each of these files.
So the first content, the second content and the third content.
So we're going to write a program, and when we execute that program, the program is going to generate
these three files inside this files directory.
And each of the files is going to contain.
The corresponding content.
So the first file is going to contain that first content.
The second file is going to contain that second content and the third file is going to contain the third
content.
So let's see how we can create this program which generates these three files.
Before we begin, I want you to create a python file and write this content.
So two lists, three items each.
You can write some dummy content, whatever you like for these strings.
And I would also want you to create a file directory under the main project directory and just leave
it empty.
The directory will be populated by Python with these three files.
The python file itself can be anywhere.
It can be in the main directory.
Where main that point is, or it can be in another sub directory, as in my case, it is here on the
bonus.
Bonus six.
With that, let's begin coding.
So how do we create multiple files?
Well, when we say the word multiple, that means we need a full loop which iterates over content.
And over file names.
So we're looking at two variables at the same time.
We need to.
Fetch this, and that's in the first iteration.
Then in the second iteration, fetch that.
So get that.
And this one.
And to be able to do that, we use these two variables and we iterate over.
Well, should we use enumerate?
It doesn't sound like the solution to me because enumerates creates.
These kinds of objects.
So a multiple.
To pulse like that.
And each couple has the index as the first item and the item itself as the second item.
But this time we're not interested about indices.
We're interested.
On the items of that and that.
And to do that, we use zip the zip function, which works basically like enumerate, and I'll demonstrate
you how.
So if we have lists.
A and list B, 1020, 30.
Then X is going to be zip A and B.
So that's what we enter here as well.
Zip.
Contents and final names.
So not content and file name, but contents and file names.
These two lists here as well.
We have these two lists.
So if you print out zip, you get this representation of the object.
But if you convert it into list, as we did with the enumerate object, then you get this.
Representation, which is the same structure as the structure of the enumerate object.
The difference is that the first items here are actual items of the first list, and the second items
are the items of the second list.
So then we can continue our full loop here.
We create a file object in each iteration and use the open function.
So the file path.
Now, what is file path going to be?
The five paths are in five names.
So we're going to use file name, file name extracts doc txt in the first iteration of both.
In my case, the files are going to be inside files.
Therefore, I want to construct a string.
Using an F string.
So it's going to be a file slash, and then file name is the variable.
And then close the string.
So what we're doing here is if file name is equal to the text, then this is going to create the file
path files slash dog dot text.
So a file in this path is going to be created
in right mode.
So we want to create files.
That means we want to say right here, not read.
And then file that right.
And here goes the content.
So in the first iteration, content will be this first string.
Let's try to run this run.
We get an error initially file not found, not such file or directory file slash total text.
How comes when files actually exist?
Even if I delete this files.
Because, of course, we expect the program to create them.
So if I run this again, we're going to get the same error.
So it's not related to the files being there already.
The problem is that my bonus six dot.
P file is inside this bonus directory.
So bonuses here and files are there.
So what we need to do is we first want to go one directory up using these two dots.
That's the syntax and then slash.
So that means the python file is.
Inside a bonus folder.
So we go up one level.
And at this point we are in the app one directory, which is one level up bonus.
And then we go to files.
So at this point we can access files.
If your script, if your bonus $0.06 file is in the root directory, then all you need to do is that
you don't need to write this part.
So if I run this now, we're going to get the files generated successfully.
Doctor text.
Presentation The text and reports the text.
Now, I mentioned earlier that.
This list is too long.
We can fix that by splitting these into multiple lines.
All you have to do is go after the first comma and press.
Enter and then go to the second comma press enter and the list will be spread out in multiple lines.
It's still a list.
The object is exactly the same.
So this is also part of the syntax.
Another way to create a list if another program.
It will run again.
So we still have these files.
These are always overwritten whenever we run the program because the right method overrides any existing
files.
So that's how you can create multiple lines of code for one single expression.
So the expression in this case is this list construction, and you can also split the string if you
want.
So if the string was very long and maybe you want to split this here, you can just put your cursor
there, press enter and Char will split it.
If you are on another ID, then maybe this will not work automatically.
So perhaps you you want to do this manually.
So the way you split the string is you just close the first string where you want it.
And then.
Close the quote.
So you open quotes here, you open quote there, and then in the next line again, you open the quotes
and you close the quote.
So so all of that is treated as one string.
So it is one single string.
Note that this will not work in the Python console.
So if you want to have a string in multiple lines and you execute, that string is actually going to
be executed.
It is going to be stored in the Python current session.
So this split only works when you are working on that P Files.
Now, if the string was not in a list, it would have to be split differently.
So let's suppose that A is one single string I must string on my own.
So when strings are individual, you can still press, enter there and split it.
A birds python pie charm actually will add this backslash.
And that means this is still one string.
So if the string is outside of the list, a backslash is used.
Additionally, both parts of the string are still.
Enclosed in quotes both.
There is a backslash in the first line, and if you want to keep splitting, you want to add another
backslash and the other part of the string there and so on.
So this is the case when you have very long strings that go beyond the horizontal space of your editor.
And it becomes useful to split them like that.
So the code remains easier to to see, to read and manage.
And that's about this example.
Thanks a lot for following.