我正在用 python 中的 sys.argv 做一些事情,这里是代码:
年龄1.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
如果我在 bash 中像这样运行代码:python age1.py xxxx,程序运行良好
,但如果我像 ./age1.py xxxx 那样运行,鼠标将变成像“+”这样的符号,然后程序结束:
"./age1.py: line 5: syntax error near unexpected token `sys.argv'
./age1.py: line 5: `if len(sys.argv) == 2:'
系统:Ubuntu 10.10
Python 2.7.3
有什么理由吗?