In this video, you learn how to create new text files via Python and how to add content to those text
files to create new tax files.
We use the open function as a built in function.
So you see, I didn't import anything.
The open function gets a file path as inputs and also the modes.
These are the two most important arguments.
The file path could be just a file name, such as File 1.60, so a new file with this name will be created
in the working directory of the working directory is the directory where the Python file is located.
So a new File 1.60 will be created in the same directory with mean that supplied event.
The next argument is the moods of the modes could be either W or are for non-binary files for text files.
So in our case, it is W.
If you were processing binary files such as PDA files, jpg files, etc. you'd use WB, but we are working
on takes files.
See us with files, estimable files and all the files that have text in them and text only.
So w know of this function returns a file object.
So a specific python file type, a file object and you need to store all of that file object in a variable.
So let's create a variable called file so that we'll return a file object.
In other words, we have created the file inside Python, and we can use the right methods to write
inside that file.
What do we rights?
Well, text and text is represented by strings in Python.
So let's say first the text you wrote that and you can write all the lines if you like.
You can apply that method as many times as you want.
Second text and so on.
Once you are done writing the files, you want to close the file with file that's close and then run,
and you see that file one that was generated in my working directory.
And this is the first string that we added and this is a second string.
So not very impressive, perhaps because you were expecting this to be something like this, I believe.
Well, that didn't happen because this break line is also a character, and we didn't provide that character.
So we just said, right, that text.
And then just after that, right, the other text.
So Python doesn't know that she wants a brick line.
If you want a brick line, you want to add backslash.
And that means after the first text, there will be a brick line and then comes to whatever comes after.
So we run again.
Now, if I run this, this file name will override the existing file name if there is something.
In other words, if this file path exists, it will be overwritten.
So run this and you see that now we do have this break line.
If you want to break lines, you can add another backslash and run.
And now we see this blank line in here, another way to write a book of codes is to just delete this
right methods and do.
Contends Eagles to a multi-line string, so with multi-line strings, you have this strip of quotes
in here and then triple records in here and in sides here you can write texts such as first text and
then second text.
Now you don't need to answer backslash and here, because in multi-line strings, the break line with
its year is recognized.
So for example, let's have this or maybe two lengthwise and then apply file.
That's right, and we write that string content.
So this time we just passed the variable directly run.
Check and we have one to empty lines.
However, notice that we also have a brake line here and also a brake line here.
So if you don't warn them you want to avoid the first brick line, so you want to delete that and start
directly after the quotes.
And also here.
Put the quotes directly after the lost tax run again.
And yeah, this is the puts no brake line in the first line this time.
No notes is the file that's closed methods.
This is a method that will close the file for operations and you cannot write and it takes off of that.
So let's say third tax year if we run this.
We're going to get an error it cease operation on close file, so this method basically controls the
flow of operating with files.
Now there is a better method to do the writing of a file, the creation of a file that is by using a
with context manager with works like this you see with.
Open function, so you could vet as file.
So this is a new way to bind this variable with this.
With the output of this function, so we do need that now in science, here you apply, find those rights
and your rights wherever you like.
You're right, it's once twice how many times unions and everything goes indented under the with line.
So it's a bloc of codes, and the benefits here is that you don't need this close method because once
the block is run, the file is closed automatically.
So this is a recommended way to create and handle files.
You don't need to use the closed methods, and normally you want to define the contents outside this
block.
So first, you have the contents variable.
You know what you want to write, and then you apply the right methods and you say content there, and
perhaps you need one more methods or not.
Not in this case.
So do you read that as well?
That is the final codes for this video.
So from that?
And you should see that we've got this content.
So that is the best way to write a new file in Python and add content to that file.
In the next video, I'll show you how to read text files, which is very easy.
See you there!