ItSolutionStuff.com

How to Append Text or Lines to a Text File in Python?

By Hardik Savani • October 30, 2023
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...

Tags: Python
Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

Python Create Text File If Not Exists Example

Read Now →

Python Create Text File in Specific Directory Example

Read Now →

How to Create Multiline Text File in Python?

Read Now →

Python Create an Empty Text File Example

Read Now →

How to Create Text File in Python?

Read Now →

Python Post Request with Basic Authentication Example

Read Now →

Python Copy File From one Directory to Another Example

Read Now →

Python Get First Date of Last Month Example

Read Now →

Python Get First Date of Next Month Example

Read Now →

Python Create Zip Archive from Directory Example

Read Now →

How to Check if Today is Saturday or not in Python?

Read Now →

Python PATCH Request with Parameters Example

Read Now →