从bash脚本运行时出现Python语法错误

时间:2014-09-09 13:56:27

标签: python linux bash shell

我已将Python脚本上传到linux机器,我需要通过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 :(得分:0)

快速解决方案:在您的脚本中将python2更改为python2.7

如评论中所述,原因是您的/usr/bin/python2链接到系统中旧版本的python。

答案 1 :(得分: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
在python 2.6之后可以使用

with