 Up to this point, we've put our data into lists in our program or we've asked the user to enter data at the keyboard and we've displayed the results on the screen. Now it's time to learn how to read and write data that's in files on our hard disk. We'll take a file named actors.txt with a partial list of people who have won the Academy Award sorted by first name. We'll write a program that reads the file, puts the names in the format lastname, firstname, sorts them alphabetically by lastname, and writes the results to a new file named actors sorted.txt. Here's the actors.txt file. To start the program, we'll need an empty list for our result, which we'll call actors by lastname. Next, we have to open the data file by using the open function. We'll set our input file to be the result of calling the open function with the arguments actors.txt and r. The first argument to open is the path to the file. Because the file happens to be in the same directory that our program is in, we give the file name all by itself. If the file isn't in the same directory as your program, you'll need to specify an absolute or relative path. You can find out more about that at the links shown in the description of this video. The second argument tells what we want to do with the file. In this case, the r means we're opening the file so we can read it. The value in input file is a file handle. It's a Python object used to access the file's contents. By the way, if you try to open a file that doesn't exist, such as setting bad file as the result of opening no such file for reading, you'll get a file not found error. We'll learn how to handle that kind of error in a future video. For now, let's clear the shell and continue with our program. Now that the file is open, you use a for loop to read the file contents one line at a time. For each line in our input file, and by the way, there's nothing special about the name line. It's not a reserved word. It just happens to be a meaningful variable name. Again, for each line in the input file, let's print the line. It's important to know that when you read a file, it assigns each line of input up to and including the new line character at the end of the line to the variable. That's why, when I run this program, it looks as if the file is double spaced. Again, that's because print adds a new line all by itself, and the line that we read in still had the new line character at the end. That means we need to get rid of the trailing new line to fix the problem. I'll create a new variable just to make the difference clear. We'll set actor to be line.strip, which gets rid of leading and trailing white space. Then let's print actor and run the program again, and now we're getting the lines without that extra new line character when we run the program. Let's comment out this line. It was just there for debugging. And add the code to split the name into first name and last name. Names will become actor.split, which splits on white space. Then we'll create the new name. That becomes the last name, names sub one, concatenated with a comma and a space, concatenated with names sub zero, which is the first name. We'll append that new name to the list by last name, actorsbylastname.append, the new name. And again to help us debug, let's print new name. Run the program, and the names are now listed with the last name first. The loop exits after Python has read the last line in the file. Once out of the loop, we want to close the input file by saying input file dot close. That's the close method. The file would get closed when we exited the program anyway, but this is a way to make sure things are cleaned up. We have a lot of the program finished, but you've also gotten a lot of information. If you feel a bit overwhelmed, pause the video now and take a break. Okay, now that you're back, let's comment out this debug line also. And now that we've closed the input file, we can sort our list. We'll use this method, actorsbylastname.sort. Sort is a list method that sorts lists in place. It does not create a brand new list. It changes the original. Let's print the sorted list to show that it worked. We'll print actorsbylastname and run the program, and you can see that we have the results we want. Let's clear the shell, remove that debugging output, and proceed to our last step, which is writing the list to an output file. We'll create a variable for the file handled and use the open function again. The name of the file will be actorsorted.txt, and we'll use w to indicate that we want to write a file. If this file doesn't already exist, it'll be created. If it did already exist, it gets wiped out by the new one you just specified. You have been warned. We use another loop to go through the sorted list one person at a time. For person in actorsbylastname, we write it to the output file with the write method. The argument will be the variable person concatenated with a new line character. The write method requires a single string as its argument. Unlike print, you can't put in as many arguments as you want separated by commas. It has to be a single string. Also, unlike print, it doesn't automatically add a new line. If you want a new line in your output file, you have to say so explicitly, and that's why we concatenated the new line character in our code. When you're writing an output file, it's ultra important that you close the file when the loop is done. This guarantees that any buffers the operating system is using to hold your file data will be written to the disk. There's our program. Let's put a message at the end that says file written as actors sorted.txt. To give users some indication that the program has concluded, we'll run the program. And now let's go to the file menu and open. Let's look for all files. There's actors sorted.txt. And there are the Academy Award winners sorted by last name. Those are the basics of opening files, reading files, writing files, and closing files.