Learn Python The Hard Way. Exercise 1. 5: Reading Files. You know how to get input from a user with raw_input or argv. Now you will learn about reading from a file. You may have to play with this exercise the most to understand what's going on, so do the exercise carefully and remember your checks. Working with files is an easy way to erase your work if you are not careful. This exercise involves writing two files. One is the usual ex. This second file isn't a script but a plain text file we'll be reading in our script. Here are the contents of that file. This is stuff I typed into a file. 4.3.1. Dynamically Created Static Local Pages from Python¶ For the rest of this chapter, the example files will come from the www directory under the main examples. Python Programming/Input and Output. just as if it were assigned directly in the Python script. File Input File Objects. Python includes a built-in file. I have a python script which uses a text file and manipulate the data from the file and output to another file. Basically I want it to work for any text file input. It is really cool stuff. Lots and lots of fun to have in here. What we want to do is "open" that file in our script and print it out. However, we do not want to just "hard code" the name ex. Hard coding" means putting some bit of information that should come from the user as a string directly in our source code. That's bad because we want it to load other files later. The solution is to use argv or raw_input to ask the user what file to open instead of "hard coding" the file's name. Here's your file %r: "%filenameprinttxt. Type the filename again: "file_again=raw_input("> ")txt_again=open(file_again)printtxt_again. A few fancy things are going on in this file, so let's break it down real quick: Lines 1- 3 uses argv to get a filename. Next we have line 5 where we use a new command open. Right now, run pydoc open and read the instructions. Notice how like your own scripts and raw_input, it takes a parameter and returns a value you can set to your own variable. You just opened a file. Line 7 prints a little message, but on line 8 we have something very new and exciting. We call a function on txt named read. What you get back from open is a file, and it also has commands you can give it. You give a file a command by using the . Just like with open and raw_input. The difference is that txt. Hey txt! Do your read command with no parameters!"The remainder of the file is more of the same, but we'll leave the analysis to you in the Study Drills. This chapter shows how to execute a Python script or program. The details of Python Byte Code and the Python virtual machine (PVM) are also illustrated. What You Should See. Warning. Pay attention! I said pay attention! You have been running scripts with just the name of the script, but now that you are using argv you have to add arguments. Look at the very first line of the example below and you will see I do python ex. See the extra argument ex. If you do not type that you will get an error so pay attention! I made a file called ex. Here's your file 'ex. This is stuff I typed into a file. It is really cool stuff. Lots and lots of fun to have in here. Type the filename again: > ex. This is stuff I typed into a file. It is really cool stuff. Lots and lots of fun to have in here. Study Drills. This is a big jump so be sure you do this Study Drill as best you can before moving on. Above each line, write out in English what that line does. If you are not sure ask someone for help or search online. Many times searching for "python THING" will find answers to what that THING does in Python. Try searching for "python open."I used the word "commands" here, but commands are also called "functions" and "methods." You will learn about functions and methods later in the book. Get rid of the lines 1. Use only raw_input and try the script that way. Why is one way of getting the filename better than another? Start python to start the python shell, and use open from the prompt just like in this program. Notice how you can open files and run read on them from within python? Have your script also call close() on the txt and txt_again variables. It's important to close files when you are done with them. Common Student Questions. Does txt = open(filename) return the contents of the file? No, it doesn't. It actually makes something called a "file object." You can think of a file like an old tape drive that you saw on mainframe computers in the 1. DVD player from today. You can move around inside them, and then "read" them, but the DVD player is not the DVD the same way the file object is not the file's contents. I can't type code into my Terminal/Power. Shell like you say in Study Drill 7. First thing, from the command line just type python and press Enter. Now you are in python as we've done a few other times. Then you can type in code and Python will run it in little pieces. Play with that. To get out of it type quit() and hit Enter. Why is there no error when we open the file twice? Python will not restrict you from opening a file more than once and sometimes this is necessary. What does from sys import argv mean? For now just understand that sys is a package, and this phrase just says to get the argv feature from that package. You'll learn more about these later. I put the name of the file in as script, ex. No, that's not how you do it. Make the code exactly like mine, then run it from the command line the exact same way I do. You don't put the names of files in, you let Python put the name in. Reading and Writing Files in Python. Recommended Python Training – Treehouse. For Python training, our top recommendation is Treehouse. Treehouse is an online training service that teaches web design, web development and app development with videos, quizzes and interactive coding exercises. Treehouse has beginner to advanced Python training that programmers of all levels benefit from.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. Archives
October 2016
Categories |