繁体   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