What is the Python FileWrite method?
File handling in python is a very important concept for any programmer. In this article, we will be looking at reading and writing to files.
How to use Python FileWrite?
We are going to open a simple text file and then read and write some extra lines into it. We will be using the context manager, alternatively, we can also use commands. But when we use a context manager it will automatically close files that we have left open, this is the best practice.
So to do that, we are going to use the word with and this is going to be the start of the context manager. So we are going to type with.open(), the word open is the keyword here, because we’re opening up a file to be able to read or write to it, even if this file doesn’t exist yet.
And in this case, the file doesn’t exist yet, therefore when we open it like this to write to it, we will be creating the file in the process.
We can now give this file a name and in this example, we are going to name it, ‘jungle.txt’ because this is going to be a text file and now we need to put a comma and then some more quote marks because we want to tell the system how we want to open this file.
If we place a ‘w’ within the parentheses it is because we want to open this to be able to write to this file. So the next thing we want to do is we need to open this with as something, and in this case we’re going to do as f:
If we press enter this will indent our code. And what this means is now we don’t have to worry about closing this file. It will happen automatically for us. If we were just to open the file, it would leave it open if we didn’t do the close at the end. But this way it will do it for us. So what we can do now is we can do the f.write() to write some text to this file.
If we type in the word ‘lion’ and save. And now if we run this code, we will get no output. But if we go to Explorer, we will find a file named ‘jungle.txt’ and if we open it we will find the word ‘lion’ in it.
Now It’s important to note that if we change the word ‘lion’ to “tiger” but fail to append, it’s going to open and write over the file ‘jungle.txt’.So if we run this again, we will lose the word ‘lion’ word in the text file, since it has been replaced by the word ‘tiger’.
Appending to file with Python FileWrite
To append to a file instead of opening as ‘w’ which is the command for write. We want to open it as ‘a’, this is going to let us append to this file. Note that now because we are just appending a basic string, when we run the code it is going to and do it, it’s basically just going to put it straight after because we are just appending it right onto the end of the file. In this case, the two words are just going to appear slide by side.
We change the way the words appear by introducing a new line character ‘/n’. So when we run this, we can see that we now have the new line added in every time we append to the file.
Appending a list to file with Python FileWrite
Now we could, for example, take a list and append it to the file we have just created instead. We are going to create a list called ‘zoo’ with the following animals: [‘elephant’, ‘lion’, ‘tiger’, ‘monkey’].
Now inside the context manager here we can actually create a for loop that iterates through the list. We can say for an animal in the zoo list, and we want to do f.write(‘animal’).
And again, we want the theses on separate lines inside the text file, therefore we need to add ‘/n’ to create a new line every time a new element is added to the file.
Because we want a new file we will just overwrite the initial one and then change the ‘a’ back to a ‘w’. If we run this and proceed to open up my text fight again we will have, elephant, lion, tiger and monkey each on a new line as intended.
Now what we would do is we do a very similar thing here. We’re still going to use our context manager with open because we still want to open the file but instead of ‘w’ for write or ‘a’ for append, we’re going to do ‘r’ for read. And that’s going to let us open this file up and then read the lines. So the easiest way to read what’s in the file is just to print(f.read()).
If you’d like to see more programming tutorials, check out our Youtube channel, where we have plenty of Python video tutorials in English.
In our Programming Tutorials series, you’ll find useful materials which will help you improve your programming skills and speed up the learning process.
- How to create perfect HTML tables?
- HTML color codes
- CSS background images
- Best Programming Books You Must Read in 2021
- Python for loop
- Creating a table using HTML and CSS
- Best way of using Java Arrays and ArrayLists
- Best way of using Python Sets
- Best ways of using a Python Dictionary
- Best way of using Python Classes
- Best way of using Python Range
- Best way of using Python if-else
- Best way of using Python RegEx
- Best way of using Python Lists
- Best way of using Python Enumerate
- Best way of using Python Functions
- Best way of using Python Split
- Best way of using Python Try-Except
- Best way of using Python Tuple
- Best way of using Python Arrays
- Best way of using Python Sort
- Best way of using Python DateTime
- How to download Python?
- Best way of using Python FileWrite
Would you like to learn how to code, online? Come and try our first 25 lessons for free at the CodeBerry Programming School.