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
).