syntax error in python script

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?

Answers


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'

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

Need Your Help

how to show spine in uipageviewcontroller?

ios objective-c

I am creating a simple UIPageViewController with just two pages, how do I show the spine? Currently there is nothing in the middle. Also the outside borders are missing.

Delphi quickbooks

delphi delphi-7 quickbooks

Am trying to connect to a quickbooks company file through the qbfc library using delphi 7.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.