簡體   English   中英

如何使用python將文本附加到文件中?

[英]How do I append text to a file with python?

我正在嘗試制作一個可以連續添加“../”的文件。 我的代碼如下:

with open("/tmp/newfile.txt", "a+") as myfile:
  myfile.write('../')
  contents = myfile.read()
  print(contents)

但是,當我運行此代碼時,它返回<empty>

對於附加文件:

with open("newfile.txt", "a+") as file:
    file.write("I am adding in more lines\n")
    file.write("And more…")

對於讀取文件:

with open('newfile.txt') as f:
    lines = f.readlines()
     print(lines)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM