读取文本文件时出现语法错误

时间:2013-11-17 18:27:42

标签: python syntax-error

我无法运行以下代码,(这是Zed Shaw的“以艰难的方式学习Python”练习15):

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

我尝试从终端运行它并获得以下内容:

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

以下是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."

我正在把头撞到墙上! (Python 2.6.1,OS X 10.6.8)

1 个答案:

答案 0 :(得分:5)

python ex15_sample.txt 

为什么要告诉Python运行文本文件?你的意思不是更像

python ex15_sample.py ex15_sample.txt

或者你称之为Python程序的任何东西?