Writing Data into a Text File using Python

Free Video Tutorial and Guide

In this video, we're going to look at how to Write Data into a Text File using Python

Video Transcription

Hi, my name is Art, and I teach Python at Noble Desktop. In this video, I'm going to show you how you could write data into a text file. We'll be using Python and writing some data into a text file.

Remember that there are Python built-in functions and the function I'm talking about is the open() function. If you don't remember how to use any function in Python, you can always do help() and take a look at what it says.

You need to provide the file name or a path for a file, then there is mode. The default mode is 'r' for read. Then there is 'w' for write, and 'a' for append.

Let me show you what it does. Also, I want to remind you that computers have two types of memory: short-term and long-term. Short-term memory is where Python lives. Long-term memory is where you store your files and other data in the form of a database.

Keep in mind that you won't be communicating to the file back and forth, but rather the open() function will create a Python object that you'll be manipulating and finally saving. So I'm going to come up with some kind of variable name, like 'file'. Then I'm going to come up with a file name, like 'myfile.txt'. Since we're planning to write data into a file, I need to switch the default mode from 'r' to 'w'.

Now let's do 'phrase1' and 'phrase2'. Let's say phrase one is 'hello', and phrase two is 'give me a call next Monday'.

Now if I want to take these two phrases and write them into the file, let's go step by step. First of all, you do file.dir() if you don't know how to write data into a file. Then you see there is a method, 'write()'. What this method does is it will take some string (because, again, keep in mind that a text file is just a string) and will write the data right into that file. So I'm going to do 'file.write(phrase1)'.

Now there is another one called 'close()'. What 'close()' will do is after you manipulate the object, it will make sure that the file is actually stored and the data is actually written in the file. Sometimes it might work without 'close()', but it's a proper way of doing things.

If I run the cell again, and I get back to this file and refresh it, you'll see I'm getting 'hello'. Now if I go back and do phrase two and run this again, I get back to this file and refresh it, I'm getting 'give me a call next Monday'.

See what it does? It updates this with the same information. Obviously, you could update it with new information, but the point I'm trying to make is, 'w' and 'a' are two different things.

If you use 'w', it just writes data into a text file and you might lose data if you already have that data in the file. But 'a' will just append the data, and see what it does? It just passes it as a string. Now suppose you want to make it nice, so then what you could do is you can do the '\n', which means new line, plus phrase.

If you run it again and again, let's go back here and refresh it, you'll see I'm getting 'give me a call next Monday'. Give me a call next Monday will be on a new line.

So the point I'm trying to make is it depends on what you want to do. If you just want to write data into a text file, you go with 'w'. If you just want to append new data to the text file, you go with 'a'. And the '\n' means new line, so every time you do something, you append something as a string.

Watch my other videos and I'll show you how to fetch data from a text file and how you could read data from a text file.

How to Learn Python

Master Python with hands-on training. Python is a popular object-oriented programming language used for data science, machine learning, and web development. 

Yelp Facebook LinkedIn YouTube Twitter Instagram