简体   繁体   English

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

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

I am trying to make a file that I can continuously add '../' to.我正在尝试制作一个可以连续添加“../”的文件。 My code is as follows:我的代码如下:

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

However, when I run this code it returns <empty>但是,当我运行此代码时,它返回<empty>

For Append File:对于附加文件:

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

For Read File:对于读取文件:

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