Exercise 15: Reading Files

Everything you've learned about raw_input and argv is so you can start reading files. You may have to play with this exercise the most to understand what's going on, so do it 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 your usual ex15.py file that you will run, but the other is named ex15_sample.txt. 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.
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 ex15_sample.txt into our script. "Hard coding" means putting some bit of information that should come from the user as a string right in our program. That's bad because we want it to load other files later. The solution is to use argv and raw_input to ask the user what file they want instead of "hard coding" the file's name.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
from sys import argv

script, filename = argv

txt = open(filename)

print "Here's your file %r:" % filename
print txt.read()

print "I'll also ask you to type it again:"
file_again = raw_input("> ")

txt_again = open(file_again)

print txt_again.read()

A few fancy things are going on in this file, so let's break it down real quick:

Line 1-3 should be a familiar use of 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 we print a little line, but on line 8 we have something very new and exciting. We call a function on txt. What you got back from open is a file, and it's also got commands you can give it. You give a file a command by using the . (dot or period), the name of the command, and parameters. Just like with open and raw_input. The difference is that when you say txt.read() you are saying, "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 extra credit.

What You Should See

I made a file called "ex15_sample.txt" and ran my script.

$ python ex15.py ex15_sample.txt 
Here's your file 'ex15_sample.txt':
This is stuff I typed into a file.
It is really cool stuff.
Lots and lots of fun to have in here.


I'll also ask you to type it again:
> ex15_sample.txt
This is stuff I typed into a file.
It is really cool stuff.
Lots and lots of fun to have in here.


$

Extra Credit

This is a big jump so be sure you do this extra credit as best you can before moving on.

  1. Above each line write out in English what that line does.
  2. If you are not sure ask someone for help or search online. Many times searching for "python THING" will find answers for what that THING does in Python. Try searching for "python open".
  3. I used the name "commands" here, but they are also called "functions" and "methods". Search around online to see what other people do to define these. Do not worry if they confuse you. It's normal for a programmer to confuse you with their vast extensive knowledge.
  4. Get rid of the part from line 10-15 where you use raw_input and try the script then.
  5. Use only raw_input and try the script that way. Think of why one way of getting the filename would be better than another.
  6. Run pydoc file and scroll down until you see the read() command (method/function). See all the other ones you can use? Skip the ones that have __ (two underscores) in front because those are junk. Try some of the other commands.
  7. Startup python again and use open from the prompt. Notice how you can open files and run read on them right there?
  8. Have your script also do a close() on the txt and txt_again variables. It's important to close files when you are done with them.

Table Of Contents

Previous topic

Exercise 14: Prompting And Passing

Next topic

Exercise 16: Reading And Writing Files

This Page