Home > Blockchain >  How do I append text to a file with python?
How do I append text to a file with python?

Time:11-01

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>

CodePudding user response:

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)
  • Related