Join Joe Marini for an in-depth discussion in this video Reading and writing files, part of Learning Python.
- [Instructor] Python provides built-in methods for working with files. You can open files, write data to them, read the data back in, and so on. So basically all the things you expect to do with files. And that's what we're going to take a look at in this chapter. Let's begin by opening up the "files_start.py" file in your editor. And Python already knows about files inherently, so I don't need to import any classes to work with them. So for our first example, let's just write some data to a file. So to do that, add some code.
I'll call the open function, that takes two arguments. Alright. So to open a file, I call the open function and it takes two arguments. The first is the file to operate on, and the second argument is the kind of access that you want. So in this example, I'm opening "textfile.txt" for write access, because of the "w" parameter, and the plus sign means to create the file if it doesn't already exist. And when the function returns, I'm storing the creative file object in the "f" variable.
Now that the file is open, let's write some lines of data. So, alright, "for i in a range of 10." I'm going to take the "f" variable, call the write function on it. And I'll just write some text. Alright. So the write function writes data to the file, which in this case is just a series of lines. And then the file needs to be closed. So I'll call "f.close" when we're done. Okay so to recap, we open the file, write some data, and then close it.
So let's go ahead and try this. Alright, and let's go back to our file list. And sure enough, there's "textfile.txt." And if we open it you can see here is the lines that were written. So now let's try adding some content to the file that we just wrote. So I'm going to comment out the line that opens the file and creates it, and I'm going to write a new one.
And this will be "f equals open," and it's going to be "textfile.TXT." And in this case, I'm going to pass a different mode to it, which is "a". So the "a" parameter means to append data to the end of the file instead of overwriting all the existing content that's already in there. The rest of the code will remain the same, which essentially means that 10 new lines will be added to the existing file. Alright so I'll save and we'll go back to the debug panel and run this.
And let's go back to the final list. And you can see that 10 new lines have been added to the end. Let's close this. For the last example here, we're going to read the file contents. Now, there's a couple of ways that you can do this. First, I need to open the file for read access. I'll just simply change this "a" to an "r", and I'll also comment out the close line, and I'll comment out that writing.
Now I'm going to check to see if the file mode is equal to "r". So for this first example, I'll just use the read function to read the entire file contents, and to print them out. So I'll write contents equals f.read. And then print contents. So the reason I'm doing the "f dot" mode is equal to r here is to make sure that the file was successfully opened. If it wasn't successfully opened then there's nothing to read, so I don't want to call this code right here.
So let's go ahead and run this. And sure enough you can see here in the debug console output that the contents of the file is being printed. You can also read the contents of a file line by line. If you have a really large file, sometimes it doesn't make sense to read the whole thing in, especially if all you want is a small piece of content. So the read lines function is useful for this. I'm going to comment out this previous read call, and I'm going to replace that with fl = f.read lines, and then I'll write "For 'x' in, fl print x.
And let me comment out that last print contents call. So in this case I'm reading the content as an array of individual lines and then printing them, so the net effect should be the same that we just observed, only I'm doing it in a different way. So let's save and let's go ahead and run this. And sure enough you can see that the lines are being read in and printed out. Just like everything else, Python makes working with file content really easy.
- Installing Python
- Choosing an editor or IDE
- Working with variables and expressions
- Writing loops
- Using the date, time, and datetime classes
- Reading and writing files
- Fetching internet data
- Parsing and processing HTML