【问题标题】:Why does a Python script work on Windows and not in Linux?为什么 Python 脚本在 Windows 上运行而不在 Linux 上运行?
【发布时间】:2017-03-28 00:02:32
【问题描述】:

我一方面是 Windows 7 和 Python 2.7.12,另一方面是 Red Hat Enterprise Linux Server 版本 6.5 和 Python 2.6.6。

我有一个脚本可以在 Windows 上正常运行,但在 RHEL 上却不行。

我收到以下语法错误:

with open('pathtofile', 'rb') as f_input, open('pathtofile', 'w') as f_output:
#                                       ^   

SyntaxError: invalid syntax

可能是两个系统的Python版本不同造成的?

【问题讨论】:

    标签: python linux


    【解决方案1】:
    with open('pathtofile', 'rb') as f_input, open('pathtofile', 'w') as f_output: 
    

    Python 2.6 不支持。在该版本中,您只能在with 语句中打开一个文件。相反,您可以这样做

    with open('pathtofile', 'rb') as f_input:
        with open('pathtofile', 'w') as f_output: 
    

    【讨论】:

      猜你喜欢
      • 2019-05-25
      • 2015-02-18
      • 2023-03-18
      • 1970-01-01
      • 1970-01-01
      • 2019-10-04
      • 1970-01-01
      • 2019-09-11
      • 2016-03-10
      相关资源
      最近更新 更多