scandalous
scandalous

Reputation: 912

Writing to a new line in python in append mode

how can i append to a new line for an existing file in python ?

my code is :

        status_done="Completed"
        f = open("%s\%s%s" % (path,self.filename,filelog_ext),"a")
        data=("%s \r" % status_done)        
        f.writelines(data)

i tried \r , \n , both \r\n none of them work, it always appends to the end of the existing line

here's the output it gave me:

line1:http://www.md5.comCompleted

i want the Completed to be on a new line i.e:

line1:http://www.md5.com

line2:Completed

Upvotes: 1

Views: 11489

Answers (2)

wesinat0r
wesinat0r

Reputation: 300

You can also use

with open("File.txt", "ab") as myfile:

"ab" will make it append in binary mode which includes escaped line chars.

Upvotes: 2

Joran Beasley
Joran Beasley

Reputation: 113930

how bout

data="\r\n%s" % status_done  
f.write(data)

as further explanation you were adding your newline AFTER your "Completed" ... this just moves the newline to BEFORE your string so that your string appears on the newline

Upvotes: 6

Related Questions