Oct-12-2020, 06:36 AM
First: Use a context manager. It closes your file automatically, when the block is left.
with open(r"C:\Users\....\Documents\somefile.txt", "w") as fd: for line in lines: fd.write(line) fd.write("\n")Doing this with writelines can be done with a trick:
from itertools import repeat, chain with open(r"C:\Users\....\Documents\somefile.txt", "w") as fd: line_sep = repeat("\n") line_iter = chain.from_iterable(zip(lines, line_sep)) fd.writelines(line_iter)
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
All humans together. We don't need politicians!