Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Learn Python the Hard Way, Exercise 15

Tags:

python

I'm trying to solve exercise 15's extra credit questions of Zed Shaw's Learn Python the Hard Way but I've ran into a problem. The code is as follows:

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()

print txt_again.read()

I understand all the code that has been used, but extra credit question 7 asks:

Startup python again and use open from the prompt. Notice how you can open files and run read on them right there?

I've tried inputting everything I could think of in terminal (on a mac) after first starting up python with the 'python' command, but I can't get the code to run. What should I be doing to get this piece of code to run from the prompt?

like image 384
Robin Spiegel Avatar asked Oct 10 '11 09:10

Robin Spiegel


1 Answers

Zed doesn't say to run this particular piece of code from within Python. Obviously, that code is getting the filename value from the parameters you used to invoke the script, and if you're just starting up the Python shell, you haven't used any parameters.

If you did:

filename = 'myfilename.txt'
txt = open(filename)

then it would work.

like image 155
Daniel Roseman Avatar answered Sep 22 '22 00:09

Daniel Roseman