In this example, you will learn to append to a file.
To understand this example, you should have the knowledge of the following Python programming topics:
Open file in append mode and write to it
The content of the file my_file.txt
is
honda 1948 mercedes 1926 ford 1903
The source code to write to a file in append mode is:
with open("my_file.txt", "a") as f: f.write("new text")
The content of the file after appending a text to it is:
honda 1948 mercedes 1926 ford 1903new text
Open the file in append 'a'
mode, and write to it using write()
method. Inside write()
method, a string "new text"
is passed. This text is seen on the file as shown above.
If you want to learn more about different types of file opening modes, please refer to Python file I/O.
Related posts:
Python String casefold()
Python Dictionary fromkeys()
Python String ljust()
Python Program to Sort a Dictionary by Value
Python Dictionary update()
Python bool()
Python Statement, Indentation and Comments
Python filter()
Python Closures
Python map()
Python isinstance()
Python Program to Find Armstrong Number in an Interval
Python Program to Extract Extension From the File Name
Python Program to Represent enum
Python getattr()
Python oct()
Python dict()
Python for Programmers with introductory AI case studies - Paul Deitel & Harvey Deitel
Python Object Oriented Programming
Python String format_map()
Python @property decorator
Python print()
Python String rjust()
Python Program to Merge Two Dictionaries
Python Errors and Built-in Exceptions
Python Program to Return Multiple Values From a Function
Python 3 for Absolute Beginners - Tim Hall & J.P Stacey
Python List insert()
Python pass statement
Python Set discard()
Applied Text Analysis with Python - Benjamin Benfort & Rebecca Bibro & Tony Ojeda
Python staticmethod()