简体   繁体   中英

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>

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM