So far in this revision, we have worked with code which processes internal information.
So information inside the script itself.
So getting a string from a variable, capitalizing it.
Maybe getting some input from a function processing it.
So the python file was not communicating with any external files, but that is not always the case in
real life.
So in real applications, you have to process external files.
That's why now we need to review.
External file processing, and we're working with text files which are the most common.
Type of files.
So creating files.
How can you create a file from Python?
Well, using that code.
So the open function will create a file object and that file object will be stored in this variable.
Right?
So file will be equal to this file object.
Then in this file object, you have to write contents.
So the right method.
Belongs to this type of object.
So it's part of the file object type and X, and it expects a string.
So if I run that, that will create a bulk text file.
So this is a case of writing files.
And the key here is that you need to provide this y argument for writing.
Now, another version of writing files, writing content to a file is this year.
So this was just a one line string, a simple string.
But sometimes you may want to add.
Strings which span across multiple lines.
In that case, you want to use a multi-line string with triple quotes here and triple quotes there.
You store that in variable and then you apply that code again.
And the only difference here is that you are providing the valuable content here instead of writing
the string directly.
You can also write the string directly.
You can cut that and paste it here, but that is not very readable.
It would mess up the culture.
So whenever you have multi-line strings, please store them in a variable first and then use that variable
in your code.
Something to note is that if I was to run this script, it would create this book, that text file.
Right.
And then if I run this other script, this would override that book, the text file.
That is because the name here and the name in here are the same.
So this would overwrite that previous file.
And another thing to note is that the files will be created in the project directory.
So if you are working on Parcham, you have a dot p file where you have the code and this file will
be created in the same folder with your dot p file.
But if you're using a python console, then it's important to know where the python console is pointing
to what directory it is related to.
What did you open it from?
So the current directory of the Python console is important here because that is where the file will
be created in that directory.
Then we jumped to reading files.
The difference here is the arguments and the right methods.
And also, of course, here we store the output of this in this variable.
So this returns a string and the string represents the text of that file.
So this file has to exist in the current directory, in the project directory for you to be able to
read it.
So for example, here, if I call this variable, I'll get this.
Right.
That is the text that I wrote in here.
Up here?
No, no.
That we have these backslash and they symbolize break lines.
So when you just call the variable like that in a Python console.
You may see these as backslash ends, but if you use a print function to print that variable, then
the backslash ends will be rendered as actual break lines.
So we have a break line in here and we have a break line in here as well.
Lastly, the red Lines method.
So you see that the difference of this and that here.
Is the methods.
This is red lines.
This is.
And of course, this time I'm reading another text file.
I did create this text file up here.
Using the right lines method.
So right lines is the homologue of red lines.
Basically, with rights lines, you provides a list of strings.
But with rights, you provide a string.
So write lines will write each of those things in a separate line in the text file.
And red lines will read every line as a separate string and produce a list of those lines.
It's important to use a brake line after these lines in here if you want them to be separated in separate
lines.
Right.
Then when you read this, of course you'll read it together with those backslash ends.
As you see here, down here.
So if you want to get rid of those ends, you can use list comprehension.
So what I'm doing here is I am iterating through the items of the content variable of this list through
each of these strings.
So for each string in the content list, I am stripping out the string from those backslash ends.
So strip will remove those characters.
And so clean content.
Will contain this list now.
So clean content is the variable that we created in here.
And that's about this video.
We have one more and we are done.
So assuming the next one.