Python Student Question

Something's not working with my input exercises. I'm long past school - just learning for my interest.
The code is below and the results follow. The error has the carat under the second quotation mark in line 8.
I can't figure out what it's complaining about. Thoughts??

from sys import argv

script, filename = argv

txt = open(filename)

print "Heres 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()


...
C:\Users\admin\PycharmProjects\MessinRound1>python LPHW_CH15_ReadFiles.py abc.t
t
  File "LPHW_CH15_ReadFiles.py", line 8
    print "Heres your file %r:"  % filename
                                                ^
SyntaxError: invalid syntax
LVL 2
amigan_99Network EngineerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jmcgOwnerCommented:
I can't reproduce the error with my Linux Python installation. At first I thought that, Python being sensitive to white space, that having two spaces before the % was somehow causing a problem. But it didn't cause a problem for me.

That leads me to think that you may have some weird extra character in your file which didn't survive the cut-and-paste when you posted it here.

So the first thing to try is to cut what you posted and put that in a file and try to run it.
0
peprCommented:
It may be the case that you are running the Python 2 script using the Python 3 interpreter. I tried it with Python 3.4.3 where the error is more detailed:
  File "a.py", line 7
    print "Heres your file %r:"  % filename
                              ^
SyntaxError: Missing parentheses in call to 'print'

Open in new window

0
amigan_99Network EngineerAuthor Commented:
Thanks. I'll try to run it tomorrow with paren.
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

peprCommented:
As you use Windows, I suggest to install the latest Python 3 -- it installs also Python Launcher for Windows (the c:\Windows\py.exe and pyw.exe). Remove any Python directory from your PATH. Install the latest Python 2 also.

Then put #!python2 as the first line of your script. This way you will be able to launch it via typing its name to the command line (without explicit invoking the Python interpreter). The #! line will be used by the Python launcher to detect and use the newest installed Python 2 interpreter. The #!python3 says to use the installed Python 3 interpreter. Using that line, you can write both Python 2 and Python 3 scripts and launch them a unified way. If you need to launch it explicitly, use py myscript.py (instead of python myscript.py; py.exe is in PATH, because c:\Windows is always in PATH). The py.exe is the one that checks the #! line and launches the correct interpreter.

See https://docs.python.org/3/using/windows.html#python-launcher-for-windows for more details.
1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
amigan_99Network EngineerAuthor Commented:
I simplified the code to ...

print "How now brown cow"


C:\Users\admin\PycharmProjects\MessinRound1>python Go123.py
  File "Go123.py", line 3
    print "How now brown cow"
                                                     ^
SyntaxError: invalid syntax

print ("How now brown cow")

C:\Users\admin\PycharmProjects\MessinRound1>python Go123.py
How now brown cow


If I run the simple script in pycharm IDE either way is fine.

I looked at the output window and pycharm and say that it was running:

C:\Anaconda\python.exe C:/Users/admin/PycharmProjects/MessinRound1/Go123.py

So the python installed with Anaconda is good and as pepr suggested it's version related.

Thanks!
0
amigan_99Network EngineerAuthor Commented:
Thanks much!
0
peprCommented:
You are welcome ;)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Python

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.