running "./script" gets syntax error after import statements, but "python script" works fine

Posted by nzomkxia on Stack Overflow See other posts from Stack Overflow or by nzomkxia
Published on 2012-09-02T09:30:31Z Indexed on 2012/09/02 9:38 UTC
Read the original article Hit count: 408

Filed under:
|

I'm doing something with the sys.argv in python here is the code:

age1.py

import datetime
import os
import sys

if len(sys.argv) == 2:
    now_time = datetime.datetime.now()
    future_time = now_time + datetime.timedelta(int(sys.argv[1]))
    print "date in", sys.argv[1],"days",future_time

elif len(sys.argv) == 4:
    print "three paras"
    spe_time = datetime.datetime(int(sys.argv[1]),int(sys.argv[2]),int(sys.argv[3]))
    now_time = datetime.datetime.now()
    diff_time = now_time - spe_time
    print "days since then..." , diff_time  

if I run the code in bash like: python age1.py xxxx, the program goes fine
but if I run that like ./age1.py xxxx, the mouse will become a symbol like "+", then the program ends up with:

"./age1.py: line 5: syntax error near unexpected token `sys.argv'  
./age1.py: line 5: `if len(sys.argv) == 2:'

system: Ubuntu 10.10
Python 2.7.3
any reason for that?

© Stack Overflow or respective owner

Related posts about python

Related posts about python-2.7