1

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)

hyde
  • 60,639
  • 21
  • 115
  • 176
DTB
  • 13
  • 1
  • 4
  • Edited question a bit, but you seem to be missing some newlines in the code snippet... I didn't start guessing where they should be, please edit yourself to fix... – hyde Nov 17 '13 at 18:34
  • Thanks for the edit, @hyde. I'm not sure where the newlines would be - I cut and pasted the code from the tutorial and they seem identical to me. – DTB Nov 17 '13 at 20:56
  • This helped me lol, I was doing the same thing and stuck on the same part. (to clear it up for anybody else, need to run the proper script, with ex15_sample.txt as a parameter, but we were running it incorrectly. – Randy Martin May 16 '19 at 19:18

1 Answers1

8
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?

DSM
  • 342,061
  • 65
  • 592
  • 494