![](/img/trans.png)
[英]Python Not able to append new line character after the end of a line when writing to a new file?
[英]Writing to a new line in python in append mode
如何在python中為現有文件追加新行?
我的代碼是:
status_done="Completed"
f = open("%s\%s%s" % (path,self.filename,filelog_ext),"a")
data=("%s \r" % status_done)
f.writelines(data)
我嘗試了\\ r,\\ n,\\ r \\ n都不起作用,它總是附加到現有行的末尾
這是它給我的輸出:
第1行: http://www.md5.com完成
我希望Completed換行,即:
第1行: http : //www.md5.com
line2:已完成
怎么樣
data="\r\n%s" % status_done
f.write(data)
作為進一步的解釋,您是在“完成”之后添加換行符...這只是將換行符移動到字符串之前,以便您的字符串出現在換行符上
您也可以使用
with open("File.txt", "ab") as myfile:
“ ab”將使其以二進制模式附加,其中包括轉義的行字符。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.