Write to a File in Python

Will Button
InstructorWill Button
Share this video with your friends

Social Share Links

Send Tweet

This lesson will teach you how to write to an external file in Python, either by replacing the file completely or appending to an existing file.

We can use the Python open method to open a file for write access. I'm going to open one called cars.txt. Specify the w for the write mode. Then create a list that contains a list of cars. Then we'll iterate across that list with "for car in cars". We'll do "f" and call it a write method and pass in the name of the car.

I'm going to execute this using the Python command. Then I'm going to open up that cars.txt file. It listed our cars, but it did it all on one line. Let's clean that up. We will add a carriage return at the end of each line. We'll save that. Run it again.

When we check the contents of our file, each car is listed on a separate line, but the original line that we had is missing. Whenever you use the "w" as the file mode, that's a write mode. Write mode replaces the contents of the file every time it opens it.

If, instead, you want to append to it, you have to specify the append mode. We can do that and run this again. Now, when we take a look at the contents, you can see that it just added to the end of it.

Because my application is exiting here, the file logic's being implicitly closed by Python. In a longer running application, you need to specifically call the method to close your file. Or you can create a "with" block.

We can iterate across that, just like before. Remember to add our new line character. Whenever this "with" block exits, it will close the file for us automatically. When we take a look here, it's added on new lines to it and it closed the file for us.

It's important that you either remember to close your file or you use a "with" block that automatically closes your file for you so that that file object's not remaining in memory. Eventually, once you're done using the file, the garbage collector will come along and free up that memory, but you really don't want to rely on if or when that's going to happen.

Let me actually do another quick example here, just to show you that implicit close, whenever the program exits. I'm going to start a Python shell here and then say "f = open". We'll use that same file, "cars.txt".

Give it write access. Then do "f.write" and just say "hello". That write operation happened. If we take a look at the cars.txt file, whenever we open the file, it deleted the contents of it, but it hasn't written out the word "hello" yet.

If I close it, that's when it actually does the write operation. That's what I was referring to up here. Whenever the application exits, it implicitly calls this close method. In a longer running application, you'll see your file created, but you won't see the contents as you've written to it, because you didn't close it either by explicitly calling the close method or using a "with" block, like we did in the second example.

Working with JSON is going to be common for you as well. Let's create a JSON object called "cars". It's going to be a list that contains a list of cars. We'll say that the "make=chevy". We'll have another one where the "make=tesla". Then we'll create a third one, where the "make=porsche".

Now, we can import the Python JSON module. We'll do a "with" block and say "open ('cars.json') with write access as f:". Then we can call the json.dump method, pass in our variable of "cars" and our file object "f".

Then we'll execute that and take a look at our "cars.json" file that was created here. There's our json object that was serialized and written out to a file.