There's a small problem with the output of your list comprehension version. You create clean_lines with a list comprehension, so it's a list. What happens when you call str() with a list as an argument? You'll have to loop over the lines in clean_lines and write them to the file instead of writing the list in all at once.