首页 > 解决方案 > Python:一次打开多个文件

问题描述

我正在使用 python 2.6.6 python

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)

当我运行脚本时出现错误

蟒蛇脚本.py

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

标签: python-2.6

解决方案


推荐阅读