繁体   English   中英

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

[英]Python syntax error when running from bash script

我已经将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

有人遇到过这个问题吗?

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

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

一遍又一遍...。我会告诉你!

[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可以蟒2.6后使用

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM