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?
The trouble lies here:
When
printfinds a comma, it uses that as an argument separator, as can be seen with:In that case, the next argument needs to be valid and the sequence
% filenameis not.What you undoubtedly meant was:
as per the following transcript: