Python, чтобы открыть файл .seg

file1 = open('f1.seg', "w")
file2 = open('f2.seg', "w")

with open('ankle.seg','r') as f:
    for line in f:
        for word in line.split():
            if word == 'S0':
            file1.write(word)
        elif word == 'S1':
            file2.write(word)
hafolahbi