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)

3
  • 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... Commented Nov 17, 2013 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. Commented Nov 17, 2013 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. Commented May 16, 2019 at 19:18

1 Answer 1

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?

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.