Python 打开两个文件时with语句中的SyntaxError

Python 打开两个文件时with语句中的SyntaxError,python,python-2.7,syntax-error,python-2.6,Python,Python 2.7,Syntax Error,Python 2.6,我在下面的语句中得到编译器在Python中随机引发的“SyntaxError”异常: with open(inputFileName, 'rU') as inputFile, open(outputFileName,'w') as outputFile: ^ SyntaxError: invalid syntax 这里,inputFileName是我的构建环境中的命令行参数,它应该在调用脚本之前创建并显示。下

我在下面的语句中得到编译器在Python中随机引发的“SyntaxError”异常:

with open(inputFileName, 'rU') as inputFile, open(outputFileName,'w') as outputFile:
                                           ^
SyntaxError: invalid syntax
这里,inputFileName是我的构建环境中的命令行参数,它应该在调用脚本之前创建并显示。下面是示例代码:

try:

with open(inputFileName, 'rU') as inputFile, open(outputFileName,'w') as outputFile:
       print "do something"
except IOError as e: #(errno,strerror,filename):
        ## Control jumps directly to here if any of the above lines throws IOError.
        sys.stderr.write('problem with \'' + e.filename +'\'.')
        sys.stderr.write(' I/O error({0}): {1}'.format(e.errno, e.strerror) + '.' + '\n')
except:
    print "Unexpected error in generate_include_file() : ", sys.exc_info()[0]
我没有任何线索。请帮帮我。
我正在使用python 2.7。(python27)

分组的
with
语句需要Python2.7或更高版本。对于早期版本,嵌套以下语句:

with open(inputFileName, 'rU') as inputFile:
    with open(outputFileName,'w') as outputFile:
您得到的确切错误消息有力地证明您正在Python 2.6上运行代码,而不是2.7:

$ python2.6
Python 2.6.8 (unknown, Apr 19 2012, 01:24:00) 
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> with open(inputFileName, 'rU') as inputFile, open(outputFileName,'w') as outputFile:
  File "<stdin>", line 1
    with open(inputFileName, 'rU') as inputFile, open(outputFileName,'w') as outputFile:
                                               ^
SyntaxError: invalid syntax
>>>

$ python2.7
Python 2.7.3 (default, Oct 22 2012, 06:12:32) 
[GCC 4.2.1 Compatible Apple Clang 3.1 (tags/Apple/clang-318.0.58)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> with open(inputFileName, 'rU') as inputFile, open(outputFileName,'w') as outputFile:
... 

除了我自己,我不会用毯子;blanket except也捕获名称、内存和键盘中断异常,通常您希望退出程序。

分组
with
语句需要Python 2.7或更高版本。对于早期版本,嵌套以下语句:

with open(inputFileName, 'rU') as inputFile:
    with open(outputFileName,'w') as outputFile:
您得到的确切错误消息有力地证明您正在Python 2.6上运行代码,而不是2.7:

$ python2.6
Python 2.6.8 (unknown, Apr 19 2012, 01:24:00) 
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> with open(inputFileName, 'rU') as inputFile, open(outputFileName,'w') as outputFile:
  File "<stdin>", line 1
    with open(inputFileName, 'rU') as inputFile, open(outputFileName,'w') as outputFile:
                                               ^
SyntaxError: invalid syntax
>>>

$ python2.7
Python 2.7.3 (default, Oct 22 2012, 06:12:32) 
[GCC 4.2.1 Compatible Apple Clang 3.1 (tags/Apple/clang-318.0.58)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> with open(inputFileName, 'rU') as inputFile, open(outputFileName,'w') as outputFile:
... 

除了我自己,我不会用毯子;blanket except还捕获名称、内存和键盘中断异常,您通常希望退出程序。

是否使用python 2.6?多个上下文表达式(用逗号分隔)只有在Python 2.7中才允许使用嵌套with语句。@Pavel我使用的是Python 2.7您使用的是Python 2.7,这有点令人惊讶——您报告的错误消息与此不匹配。为了排除意外调用错误Python版本的可能性,您是否可以注释掉这段代码并添加
import sys
,然后再添加
print(sys.version)
?是否使用Python 2.6?多个上下文表达式(用逗号分隔)只有在Python 2.7中才允许使用嵌套with语句。@Pavel我使用的是Python 2.7您使用的是Python 2.7,这有点令人惊讶——您报告的错误消息与此不匹配。为了排除意外调用错误Python版本的可能性,您是否可以注释掉这段代码并添加
import sys
,然后再添加
print(sys.version)
?它是否也适用于Python2.7?我需要编写如下代码:try:with open(inputFileName,'rU')作为inputFile:with open(outputFileName,'w')作为outputFile:print“do something”是的,
try
except
部分适用于任何python版本。
with
语句不支持
except
块。好的。我现在明白了。您指的是缺少的“try:”.I更正我的查询。@learningstack:您的帖子中的缩进已被删除;如果您仍然遇到语法错误,则您不是在使用Python 2.7。您报告的语法错误是在Python 2.6上运行代码时出现的典型错误。是否也是为Python 2.7编写的?我需要按以下方式编写:尝试:使用open(inputFileName,'rU')作为inputFile:使用open(outputFileName,'w')作为outputFile:print“do something”是的,
try
except
部分适用于任何python版本。
with
语句不支持
except
块。好的。我现在明白了。您指的是缺少的“try:”.I更正我的查询。@learningstack:您在文章中的缩进已被删除;如果仍然出现语法错误,则说明您没有使用Python 2.7。您报告的语法错误是在Python 2.6上运行代码的典型错误。