Reputation: 11246
I wrote the following script, which generates a 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?
Upvotes: 0
Views: 1096
Reputation: 882716
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 1,2
1 2
In that case, the next argument needs to be valid and the sequence % filename
is not.
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'
Upvotes: 1
Reputation: 799520
You can't have a comma there.
print ("Here is your file %r: " % filename),
Upvotes: 3
Reputation: 1478
The coma is not needed, try:
filename = raw_input("> ")
print "Here is your file %r: " % filename
Upvotes: 2