Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Reading and writing text files in Python is really very simple. Let's make a working copy of files.py. files-working.py. Open that working copy and we see that we have the simple loop here that reads lines of a file and prints them to the screen. So if we run that, we see there is our lines of file, print it to the screen, and here is our input file. So, we can see that that's exactly the same. Now, if we wanted to, instead of bringing that to the screen, if we wanted to write it to a file, we could do that.
Simply change this to infile, so we can have a separate one for outfile, outfile equals open and we'll call this new.txt. We will open that for write. And we will open this one here for read. It defaults to read but if I am going to specify write, then I am going to want to also specify read. This is called infile, and all we have to do here is to say file equals outfile, And now that print is going to print the outfile instead of to the screen.
And as we don't have anything coming to the screen, I am just going to print done here. So we know that we have gotten to this point. We get some feedback on the screen. I will save that and I will run it and it says done. If I refresh the file system here, which Eclipse does not do by itself, you can see there is a new.txt and it's got our five lines of text in it. So that is just a very simple way to write to a text file. Obviously, it doesn't have to be something you are reading from another text file.
You can generate your own whatever and write it out to a text file very easily, using print and the File parameter for print. Let's say that what we are doing is not necessarily line oriented. Let's say that maybe we have a very large file with thousands of lines of text, And we don't really need to read them and write them line by line. We want to do it in some bigger chunks. So I have a file here called bigfile.txt and you will notice it is a really big file of text. It has 10,000 lines of text in it.
If we look at the properties, we can see that it's 320,000 bytes. It's 300k. So, let's go ahead and open that one instead, and call it bigfile.txt and we are going to read this in what's called buffered mode, instead of doing it line by line. We will set a buffer size and I will say that's going to be 50,000 bytes. It's 300,000 bytes, so it will give us a few chunks and then we are going to start by filling our buffer, infile.read and buffersize and then instead of this here, I am going to use a While loop, because the Read method on the file handle object is not an iterable.
So we can't use a For loop, so we are going to use a While loop. We are going to say while the length of buffer. And so as long as our buffer is not empty then we will outfile.write buffer. That's really all there is to it. We read it again in the loop, so that we are not just writing the same buffer forever. So I am going to write buffer = infile.read buffersize and after we do the write, let's just print a dot to the screen, so we have some feedback that we are doing something, dot, end, equals nothing.
So this will just print dots on the screen right next to each other, and we are all done. Before the Done, we are going to want to print a blank line. So we will go ahead and save that and run it and we see we have got a handful of dots there. It means that we have written something and we refresh our file system and take a look at the properties of new.txt and it's exactly 320,000 bytes. We will open that up and there is all of our lines of text in there. So what we have done here is we simply read it in a few big chunks and obviously 50,000 bytes is very small by today's standard.
Your buffers can be bigger than that or if you are on a mobile device or something small, maybe you do want them that small. We just did that this way for illustration, but this shows you how you can read and write in buffered I/O mode. So it allows you to deal with bigger chunks of files than just line by line. So that's how simple it is to read and write text files in Python and we will discuss reading and writing binary files in a separate lesson.
Get unlimited access to all courses for just $25/month.Become a member
61 Video lessons · 99323 Viewers
56 Video lessons · 112585 Viewers
71 Video lessons · 81405 Viewers
131 Video lessons · 39078 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.