Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python write line by line to a text file [duplicate]

Tags:

python

I am trying to output the result from a Python script to a text file where each output should be saved to a line.

f1=open('./output.txt', 'a')
f1.write(content + "\n")

When I open output.txt with the regular notepad the results look like this:

color amber color aqua color analysis color app color adobe color alive app

However, when I open the file with notepad++ it looks fine and each word is saved on a line.

How can make the script save the result line-by-line so it would show the same on the regular notepad?

like image 557
user2334436 Avatar asked Feb 28 '16 17:02

user2334436


People also ask

How do I remove duplicate sentences from a paragraph in Python?

We use file handling methods in python to remove duplicate lines in python text file or function. The text file or function has to be in the same directory as the python program file. Following code is one way of removing duplicates in a text file bar. txt and the output is stored in foo.


1 Answers

You may want to look into os dependent line separators, e.g.:

import os

with open('./output.txt', 'a') as f1:
    f1.write(content + os.linesep)
like image 125
AChampion Avatar answered Oct 10 '22 12:10

AChampion