Use line break with the appropriate exhaust for this, just add '\n'
at the end of the string. In the example below I read the strings from a list and add the line break when I write to the file:
arquivo = open("arquivo.txt", "a", newline="")
persons = ['Jose antonio de oliveira; [email protected]',
'Ana Fake da Silva; [email protected]']
for p in persons:
arquivo.write(p+'\n')
arquivo.close()
Showing the file contents with the command cat
(Linux):
cat arquivo.txt
Jose antonio de oliveira; [email protected]
Ana Fake da Silva; [email protected]
Edited
After reading the comments more carefully, I thought I should complement the answer.
Reading the file:
With readlines()
:
lines = open('arquivo.txt','r').readlines()
print (lines)
['Jose antonio de oliveira; [email protected]\n', 'Ana Fake da Silva; [email protected]\n']
With splitlines()
:
lines = open('arquivo.txt','r').read().splitlines()
print (lines)
['Jose antonio de oliveira; [email protected]', 'Ana Fake da Silva; [email protected]']
See that splitlines()
, removes "automatic" the exhaust for line breaking.
At the last printed value, you can not put
%s\n
?– Woss
The problem with doing this is that n goes along with the .txt. file ex: "Max;6,000; n"
– Kenneth Anderson
That’s how it worked for me: https://repl.it/Jbl1/3. Basically I put the
file.write("\n")
at the end ofif
.– Woss
@Kennethanderson, you take that easy
\n'
with rstrip, example:'test string\n'.rstrip()
– Sidon
Or you can use
splitlines()
to remove line breaks, when you read the file. See in the answer editing.– Sidon