Append content to a file in Python

Here, we will learn how to append content to a file using write() method in Python?
Submitted by Shivang Yadav, on February 11, 2021

Program Statement: Python program to append contents to file using write() method.

Program Description: We will append the data to the file using write() method in python.

We will use the concepts of file handling in python to append the contents to the file using write() method.

  • The write() method is used to write some text to the file.
  • For appending the contents to the end, we will open the file using 'a' mode.

Steps to append contents of file:

  • Step 1: Open the file in append mode using 'a'.
  • Step 2: Get the input data from the user and store it.
  • Step 3: write the contents to the file using write() method.

Program to illustrate the solution of the problem


   id=input("Enter Id:")
   name=input("Enter Name:")
   salary=input("Enter Salary:")


   ch=input("Continue y/n?")



Enter Id:10032
Enter Name:John Does
Enter Salary:45000
Continue y/n?y
Enter Id:10323
Enter Name:Ram
Enter Salary:50000
Continue y/n?n

File Contents (data.dat):
10032,John Doe,45000

In the above code, we have opened the file for writing using 'a' mode. After this we have taken input from the user. And using the write() method, we have appended the data to the file.

Python file handling programs »


Comments and Discussions!

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© some rights reserved.