DTB
DTB

Reputation: 13

Syntax error in reading text file

I'm having trouble in getting the following code to run, (it's exercise 15 from Zed Shaw's "Learn Python the hard way"):

from sys import argv

script, filename = argv

txt = open(filename)

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

print "Type the filename again: " file_again = raw_input("==>")

txt_again = open(file_again)

print txt_again.read()

txt.close() txt_again.close()

I try to run it from the terminal and get the following:

dominics-macbook-4:MyPython Dom$ python ex15_sample.txt
  File "ex15_sample.txt", line 1
    This is stuff I typed into a file.
                  ^
SyntaxError: invalid syntax

Here's the contents of 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'm banging my head off the wall! (Python 2.6.1, OS X 10.6.8)

Upvotes: 1

Views: 4837

Answers (1)

DSM
DSM

Reputation: 353059

python ex15_sample.txt 

Why are you telling Python to run a text file? Don't you mean something more like

python ex15_sample.py ex15_sample.txt

or whatever you've called your Python program?

Upvotes: 8

Related Questions