Python:一次打开多个文件

时间:2018-05-30 11:05:04

标签: python-2.6

我正在使用python 2.6.6 蟒

Python 2.6.6 (r266:84292, Aug 18 2016, 15:13:37)

下面是script.py文件,我在打开2个文件时遇到问题。

import os
path = '/home/test/testData/log/'
os.chdir(path)

k = 0
word = 'Log ends'
filename1 = 'test.log'
filename2 = 'tes2.log'
with open(filename1) as firstFile, open(filename2) as secondFile:
    for line in firstFile:
        if word in line:
            print(line)

当我运行脚本时出现错误

python script.py

  File "script.py", line 11
    with open(filename1) as firstFile, open(filename2) as secondFile:
                                     ^
SyntaxError: invalid syntax

0 个答案:

没有答案