[英]syntax error in python script
I wrote the following script, which generates a SyntaxError
:我编写了以下脚本,它生成了SyntaxError
:
#!/usr/bin/python
print "Enter the filename: "
filename = raw_input("> ")
print "Here is your file %r: ", % filename
txt = open(filename)
print txt.read()
txt.close()
Here is the error:这是错误:
File "ex02.py", line 4
print "Here is your file %r: ", % filename
^
SyntaxError: invalid syntax
How should I fix this?我应该如何解决这个问题?
You can't have a comma there.那里不能有逗号。
print ("Here is your file %r: " % filename),
The coma is not needed, try:不需要昏迷,请尝试:
filename = raw_input("> ")
print "Here is your file %r: " % filename
The trouble lies here:问题出在这里:
print "Here is your file %r: ", % filename
^
When print
finds a comma, it uses that as an argument separator, as can be seen with:当print
找到一个逗号时,它使用它作为参数分隔符,如下所示:
>>> print 1,2
1 2
In that case, the next argument needs to be valid and the sequence % filename
is not.在这种情况下,下一个参数需要有效,而序列% filename
则无效。
What you undoubtedly meant was:你的意思无疑是:
print "Here is your file %r: " % filename
as per the following transcript:根据以下成绩单:
>>> filename = "whatever"
>>> print "file is %r", % filename
File "<stdin>", line 1
print "file is %r", % filename
^
SyntaxError: invalid syntax
>>> print "file is %r" % filename
file is 'whatever'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.