How to Append Text or Lines to a Text File in Python?
Hi Dev,
In this tutorial, you will discover how to append text in text file python. It's a simple example of how to add string to text file python. you'll learn python append text to text file. you'll learn python edit text file.
If you have an existing text file and you want to append some text or lines to the file in python then I will give you a very simple example for an editing a text file. we will take readme.txt file and append some text on it. we will use open() with "a" option. so let's see the below example.
You can use these examples with python3 (Python 3) version.
Example 1:
main.py
# append text on existing text file with open("readme.txt", 'a') as f: f.write('New text file content line!') print("New text file created successfully!")
Output:
It will append above line readme.txt file.
Hi ItSolutionstuff.com! New text file content line!
I hope it can help you...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- Python Create Text File If Not Exists Example
- Python Create Text File in Specific Directory Example
- How to Create Multiline Text File in Python?
- Python Create an Empty Text File Example
- How to Create Text File in Python?
- Python Post Request with Basic Authentication Example
- Python Copy File From one Directory to Another Example
- Python Get First Date of Last Month Example
- Python Get First Date of Next Month Example
- Python Create Zip Archive from Directory Example
- How to Check if Today is Saturday or not in Python?
- Python PATCH Request with Parameters Example