JayGatz
JayGatz

Reputation: 271

convert all lines in file to lowercase then write to new file

I am trying to read in a file and convert its contents to lowercase then write it back out all lowercase in the same fashion / order ( one word per line and the lines are in alphabetical order ) to a new file.

Ultimately, what I am trying to do is to convert all the lines in this life to lowercase letters.

Why isn't this python script doing this and how can I do this in Python 2.7:

from itertools import chain
from glob import glob

lines = set(chain.from_iterable(open(f, 'rU') for f in glob('./files/*.txt')))

with open('listTogether.txt', 'w') as out:
    for line in lines:
         line.lower()
    out.writelines(sorted(lines))

Upvotes: 1

Views: 7055

Answers (1)

karthikr
karthikr

Reputation: 99640

You are not replacing the line.lower() back into the list.

Try:

lines = [line.lower() for line in lines]
with open('listTogether.txt', 'w') as out:
    out.writelines(sorted(lines))

Upvotes: 3

Related Questions