 Up to this point, we've put our data into arrays in the program, or we've asked the user to enter data at the keyboard. In this video, we're going to create a file that has data in it, and we'll write a program that reads that data, manipulates it, and then writes a new file to disk. Here's a file named actors.txt that has a partial list of actors and actresses who have won the Academy Award. It's sorted by first name. We want to write a program that reads that file, puts the names in the format lastname, firstname, sorts them alphabetically by lastname, and writes the results to a new file called actors.sorted.txt. First, we did an empty list for our result. Actors by lastname equals the empty list. Next, we have to open the file by using the open function. We'll set our input file to be the result of calling open with the arguments actors.txt and r. The first argument to open is the path to the file. Since the file happens to be in the same directory that the program's 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 that are 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 that we can read it. And the result in input file is a file handle. It's a Python object that's used to access the file's contents. By the way, if you try to open a file that doesn't exist, such as saying bad file equals open of no such file.txt for reading, you'll get a file not found error. And we'll talk about how to handle that kind of error in a subsequent video. Now that the file is open, we can use a for loop and read the file contents one line at a time. For line in input file, this for loop will read the file one line at a time and assign each line of input up to and including the new line character to the variable line. So if I were to print line in the loop and then run the program, you would see that everything looks as if it's double spaced because print adds a new line and the line that we read in from the file still has the new line character at the end. That means we need to strip the rephrase. That means we need to strip the trailing white space to fix the problem. And I'll create a new variable just to make the difference clear. We'll say actor equals line.strip, which gets rid of the new line, and then let's print actor. And now we are getting the lines without that trailing new line. Let's comment out this print statement. It was just there for debugging. And add the code to split the name and put it together in last name first order and append that result to the list by last name. We'll say names equals actor.split. The new name is going to be names sub one, which is the last name, plus a comma blank, plus names sub zero, which is the person's first name. Then we'll say actors by last name dot append new name. And to debug, let's print new name. Clear the shell and run. And it looks like I have an error because I forgot my plus sign here. Let's put the plus sign here to make it right. Clear the shell and run again. And now we have the names in last name, comma first name order when we're out of the loop. After we've read the file, we close it by saying input file dot close. It would close when we exited the program anyway, but this is a nice way to make sure that we have things cleaned up. Now I'm going to sort the file by last name and say actors by last name dot sort. The sort method for lists sorts in place. It does not create a brand new list. It changes the original. Now I need to open an output file for writing. The output file will be, again, open. We'll give the file name actors sorted dot txt. And this time we'll say w because we want to write a file. If this file doesn't exist already, it'll be created. If it did already exist, it gets wiped out by the new one that you just specified, so you have been warned. Now we'll write a loop to go through the sorted list one person at a time and write it to the output file with the write method. For person in actors by last name we'll call the output files write method and give it the person plus a new line character. The write method requires a single string as its argument. Unlike print, you can't put as many arguments as you want separated by comm as it has to be a single string. Also unlike print, it doesn't add a new line automatically. So if we want a new line in our file, we have to explicitly say so and that's why we put the backslash in there to explicitly put a new line into our output file. When you're writing an output file, it's ultra important that you close the file. 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 print of file written as actors sorted.txt to give us some indication that the program has concluded. Let's clear the shell and run it. And if we go here and look at the actors sorted.txt there are Academy Award winners sorted by last name.