From the course: Programming Foundations: Beyond the Fundamentals
Working with file input and output - Python Tutorial
From the course: Programming Foundations: Beyond the Fundamentals
Working with file input and output
- Data input and output can take a number of forms. At its most basic, a program can simply prompt a user to enter data and then work with the user's response. In Python, you can collect user info at the command line using the input method, which pauses your program and gives the user the opportunity to type a response. When the user presses enter, the program resumes and it can then incorporate the user's response into any processing and output. But many tasks require more data than a user can enter by typing. In these situations, your program can access other methods of input, like opening a file from your computer. For instance, to use a word processing program, like Microsoft Word or Google Docs, you often start by opening an existing file that you've saved on your computer or in the cloud. When the processing is done, your program can write the results back to the same file or to another file. This preserves the results of the program's work even after the program finishes executing. This is the add-values.py file, which is in the begin folder for this video. This file contains a simple Python program that reads input from an external file, performs some processing and then writes output to another file. This program uses a few Python methods that are specific to input and output. The code in the first couple lines uses the open method. This opens files with specific names that are located in the same folder as the program file. I can see the values.txt file in the files list. Opening this file, I can see that it contains a list of numbers. I also noticed the file list doesn't contain a values-total.txt file. When a file doesn't exist, Python will create it. The program will use the new values-total.txt file to store the output of this program. Line three and line 10 both use a simple print statement, which is one way of providing output, but simply passing a string to the print method sends that output to the command line. That data is temporary, and when a user exits a terminal, the output is gone. The for loop is doing something different. It's taking one line of the input file at a time, which is one number at a time. Now, that number is encoded as a string, so it's converting that to an integer, and then adding it to the sum variable. When the program has looped through all of the lines in the values.txt file, the sum variable should contain the total of all the numbers. The for loop also includes a print statement, which is printing the current line with any line ending character stripped out. But there's an extra argument here that specifies that the data should be printed to a file and specifies the out file, which is the values-total.txt file. So when I'm done, I should see the list of numbers recreated in the output file. After the loop is completed, there's one other print command. This prints the word total and a comma, followed by the value of the sum variable converted to a string. And this print statement is also directed to values.txt. Finally, we have the close statement, which indicates that the program is finished writing to the values-total.txt file. When I run the file, I get an error in the terminal. This is an error you may or may not run into when you're following along, depending how your editor's configured, but if you're also running into this error, we can fix it pretty easily. Let's go to the settings menu, and I'm on a Mac, so if you're on a PC, this will look a little different. And then in the search settings box, I'm going to search for Python and execute and my editor shows me a few commands for Python that relate to executing code and this is the one that's going to help me fix my error. Execute in File Dir or File Directory. So I check that and this just tells the editor that when I run a Python file and it mentions another file, in this case, values.txt, it should look in the same directory for my other file. So I'll close up my settings and let me try and run my code again. Now, when I run my code, I see the statement's processing input and output complete in the terminal almost immediately. The processing my program is doing is pretty minimal but if it was more involved, statements like this could be useful to let the user know where in the process my program is. Over in the file list, I also notice the newly created values-total.txt file. I'm going to open that up and I see the same list of values as in values.txt along with the total at the bottom. So the result of my program's processing has been written to a new file and saved. Sending output directly to the user interface or saving to a file both have their uses. What kind of program could you create that interacts directly with a user? Where could writing output to a file really be valuable? As you're putting your programming skills into practice, let your imagination guide you. Try things out and have fun with it.