从bash脚本运行时出现Python语法错误
我在一台Linux机器上上传了一个Python脚本,需要通过一个bash脚本来运行它。当我直接输入“python test.py”时,脚本运行得很好,输出也正常。但是当我运行bash脚本“bash runScript.sh”时,却出现了语法错误。
我的Python脚本(非常简单):
with open ("TextFiles/10.10.10.10config.txt",'r') as f:
print f
我的bash脚本:
wget --no-check-certificate https://10.10.10.10/config.txt -O /usr/bin/grabber/TextFiles/10.10.10.10config.txt
/usr/bin/python2 /usr/bin/grabber/test.py
当我运行bash脚本时,出现的错误信息是:
File "/usr/bin/grabber/test.py", line 1
with open ("TextFiles/10.87.4.4channel_config.txt",'r') as f:
^
SyntaxError: invalid syntax
有没有人遇到过这个问题?
2 个回答
0
又来了……我来给你演示一下!
[server]$ /usr/local/bin/python2.7 -c "with open('test.py','r') as f: print 'OK'"
OK
[server]$ /usr/local/bin/python2.5 -c "with open('test.py','r') as f: print 'OK'"
<string>:1: Warning: 'with' will become a reserved keyword in Python 2.6
File "<string>", line 1
with open('test.py','r') as f: print 'OK'
^
SyntaxError: invalid syntax
with
这个东西是在 Python 2.6 之后才可以用的。
0
快速解决办法:把你的脚本中的 python2
改成 python2.7
。
原因是,正如评论中提到的,你的 /usr/bin/python2
其实是指向你系统中一个旧版本的 Python。