0

Python is seeing some problem with how I am opening a file with the code below

if __name__ == "__main__":
    fileName = sys.argv[1]
    with open(fileName, 'r') as f:
       for line in f:
           print line

It is producing the error

./search.py: line 3: syntax error near unexpected token `('
./search.py: line 3: `  with open(fileName, 'r') as f:'

Am I missing an import? What could be the cause of this?

EDIT: OS - CentOS, Python version 2.6.6

Not sure how I installed, I am running an image from a .edu openstack site. Not sure of the distribution, binaries, ...

smci
  • 26,085
  • 16
  • 96
  • 138
KDecker
  • 6,124
  • 7
  • 29
  • 68
  • As @enrmarc says, it disliked line 3, so suspect line 2... – smci Apr 04 '14 at 00:28
  • I just tried it and it worked. Could you please show us how do you call your py file? – Bruno Gelb Apr 04 '14 at 00:29
  • The file is name search.py, I call it with the command `./search.py InputFile` – KDecker Apr 04 '14 at 00:31
  • **How did you install? source? binaries? distribution? which ones, from where?** – smci Apr 04 '14 at 00:33
  • Also, do `which python`, to make sure you're not picking up another Python on the UNIX path. – smci Apr 04 '14 at 00:33
  • 1
    @BumSkeeter see my answer. You have to do `python search.py InputFile` – Marcos Apr 04 '14 at 00:34
  • 1
    @enrmarc: either OP can do 1) `python search.py ...` or else 2) the first line of search.py should [be a #! comment specifying the path to the python executable](http://stackoverflow.com/questions/2429511/why-do-people-write-usr-bin-env-python-on-the-first-line-of-a-python-script) – smci Apr 04 '14 at 00:37
  • OP: I posted that same answer independently of the others at the same time, here in the this comment thread then also in my answer. So kindly upvote. – smci Apr 04 '14 at 00:45

3 Answers3

3

You must add import sys in order to use sys.argv. Check this out.

I have tried this:

chmod u+x yourfile.py
./yourfile.py

and it gives me:

./jd.py: line 4: syntax error near unexpected token `('
./jd.py: line 4: `    with open(fileName, 'r') as f:'

If you are doing ./search.py file then add at the beginnig of your file #!/usr/bin/env python. Otherwise, use python file.py input

Marcos
  • 4,607
  • 7
  • 29
  • 57
  • 1
    I've copy pasted your code and it works perfectly. Could you describe your environment? (os, python version, etc). – Marcos Apr 04 '14 at 00:29
  • @BumSkeeter: post more of your file please. Maybe your PYTHONPATH or install are messed up. Can you import anything else successfully, e.g. `import re`? – smci Apr 04 '14 at 00:29
  • @BumSkeeter: please add this crucial stuff inside the question. Not here as comments on one answer. Also, **How did you install? source? binaries? distribution? which ones, from where?** – smci Apr 04 '14 at 00:32
  • 1
    Yes I am calling it with `./` not `python` – KDecker Apr 04 '14 at 00:35
  • So use `python`. Otherwise add `#!/usr/bin/env python` at the beginning of yout file. – Marcos Apr 04 '14 at 00:36
  • Yep. Thats what I was missing. – KDecker Apr 04 '14 at 00:36
  • @BumSkeeter: OP, I independently wrote you this same answer in the main comment thread. It's not ok to splatter small pieces of your question all over comment threads, especially directed to one user. You go edit the question itself and keep reediting until it's clear. It's not ok to do a hit-and-run, ignore the people that helped you, then move on. I voted to close because of your behavior. Don't do this again. – smci Apr 04 '14 at 00:49
2

The problem is that you aren't running your program with Python at all! When you do ./script (assuming that script is a text script, and not a binary program), the system will look for a line at the top of the file beginning with the sequence #!. If it finds that line, the rest of the line will be used as the interpreter of that script: the program which runs the script. If it doesn't find that line, the system defaults to /bin/sh.

So, basically, by omitting the magic line #!/usr/bin/python at the top of your script, the system will run your Python script using sh, which will produce all sorts of incorrect results.

The solution, then, is to add the line #!/usr/bin/python (or an equivalent line, like #!/usr/bin/env python) to the top of your Python script so that your system will run it using Python. Alternately, you can also always run your program using python search.py, instead of using ./search.py.

(Note that, on Linux, filename extensions like .py mean almost nothing to the system. Thus, even though it ends with .py, Linux will just execute it as if you wrote /bin/sh search.py).

nneonneo
  • 154,210
  • 32
  • 267
  • 343
0

Either:

  1. the first line of search.py should be a #! comment specifying the path to locate the python executable, usually [#!/usr/bin/env python](Why do people write #!/usr/bin/env python on the first line of a Python script? on-the-first-line-of-a-python-script). Usually this is #!/usr/env/bin python . Don't use a hardpath e.g. #/opt/local/bin/python2.7

  2. or else you can invoke as python yourfile.py <yourargs> ...


PREVIOUS: If import sys fails, post more of your file please.

Maybe your install is messed up.

Can you import anything else successfully, e.g. import re?

What are your platform, OS and Python version? How did you install? source? binaries? distribution? which ones, from where?

Community
  • 1
  • 1
smci
  • 26,085
  • 16
  • 96
  • 138
  • I have been running other files all day, I have imported hashlib, sys, re in other files in the same dir – KDecker Apr 04 '14 at 00:33
  • Obvious things to try: close shell, open a new shell, try it again. Log out, login again, try it again? Did that fix it? Did your ~/.cshrc or ~/.profile get modified? – smci Apr 04 '14 at 00:35
  • Im not good enough with unix based systems to know what those files even do. So I doubt I modified them. But I found the answer above. It was much simpler. – KDecker Apr 04 '14 at 00:37
  • I already posted that answer in the comments on main thread, independently of the others. So kindly upvote. – smci Apr 04 '14 at 00:42