如何使用python在新行中写入文件?

时间:2016-03-26 14:30:59

标签: python debian

我正在尝试创建一个将新别名写入bashrc的脚本,但是当我运行脚本时,它会写入第一行或最后一行。我的脚本非常简单:

with open ("/user/.bashrc","a+") as f1:
   f1.write(new_alias + " " + alias_command)
   f1.seek(0,0)               # This writes in the first line and (1,0) writes in the last
   command = f1.read()
   print command              # To show me the input without getting into the file every time

所以我想要它做的是当我想输入一个新的别名时创建一个新的行,这样它就不会一起涂抹并产生不必要的错误。

1 个答案:

答案 0 :(得分:0)

使用\n可能是最简单的:

with open ("/user/.bashrc","a+") as f1:
   f1.write(new_alias + " " + alias_command + "\n")