Python Append To File

The code below is an example on how to append to an existing file using Python.
The textfile will result in text being appended at the end.

#
# Python Append To File
#

def main():  

  #Append to an already created file
  f = open("textfile.txt", "a")

  # write some lines of data to the file
  for i in range(10):
    f.write("This is a line " + str(i) + "\r\n")
  
  # close the file when done
  f.close()

    
if __name__ == "__main__":
  main()


---------------------------------------
textfile.txt
This is a line 0

This is a line 1

This is a line 2

This is a line 3

This is a line 4

This is a line 5

This is a line 6

This is a line 7

This is a line 8

This is a line 9

This is a line 10

This is a line 11

This is a line 12

This is a line 13

This is a line 14

This is a line 0

This is a line 1

This is a line 2

This is a line 3

This is a line 4

This is a line 5

This is a line 6

This is a line 7

This is a line 8

This is a line 9

Comments

Popular posts from this blog

How to write to a file in Kotlin

Python Tkinter example