• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
PythonForBeginners.com

PythonForBeginners.com

Learn By Example

  • Home
  • Learn Python
    • Python Tutorial
  • Categories
    • Basics
    • Lists
    • Dictionary
    • Code Snippets
    • Comments
    • Modules
    • API
    • Beautiful Soup
    • Cheatsheet
    • Games
    • Loops
  • Python Courses
    • Python 3 For Beginners
You are here: Home / Basics / Append Text to File in Python

Append Text to File in Python

Author: Aditya Raj
Last Updated: March 7, 2022

While doing file operations, we might need to append text to an existing file without erasing the existing data. In this article, we will discuss how we can append text to a file in python.

Append text to file using write() method

To append a text to a file using the write() method, we first need to open the file in append mode. For this, we will use the open() function with the file name as its first parameter and “r+” as the second parameter. After opening the file, we can simply append the text to the file using the write() method. The write() method is invoked on a file object and takes the text that need to be appended to the file as its input parameter. You can observe this entire process below.

myFile = open("sample.txt", mode="r+")
print("The content of the file before modification is:")
text = myFile.read()
print(text)
myString = "This string will be appended to the file."
myFile.write(myString)
myFile.close()
myFile = open("sample.txt", "r")
print("The content of the file after modification is:")
text = myFile.read()
print(text)

Output:

The content of the file before modification is:
This is a sample file.
The content of the file after modification is:
This is a sample file.This string will be appended to the file.

After appending the text to the file, don’t forget to close the file. Otherwise, the content will not be saved. Here, we have used the read() function to verify the contents of the file before and after appending the text.

Append text to file using the print() function

Normally, when we use the print() function, it prints the values to the standard input. However, we can also use the print() function to append text to a file in python. The print() function has an optional parameter “file”. Using this parameter, we can specify where to print the values that are passed as input to the print() function. 

To append the text to the file, we will first open the file in append mode using the open() function. After that, we will pass the text and the file object to the print function as the first and the second input arguments respectively. After execution of the print() function, the text will be appended to the file. 

myFile = open("sample.txt", mode="r+")
print("The content of the file before modification is:")
text = myFile.read()
print(text)
myString = "This string will be appended to the file."
print(myString, file=myFile)
myFile.close()
myFile = open("sample.txt", "r")
print("The content of the file after modification is:")
text = myFile.read()
print(text)

Output:

The content of the file before modification is:
This is a sample file.

The content of the file after modification is:
This is a sample file.
This string will be appended to the file.

In the output, you can observe that myString has been appended in the file in a new line. When we did the same operation using the write() method, myString was appended to the last line of the existing file. So, you can use this difference to choose the proper approach according to your requirement. Also, Make sure that you close the file after appending the text to it. Otherwise, the changes will not be saved. 

Conclusion

In this article, we have discussed two ways to append text to a file in python. To learn more about file operations, you can read this article on file handling in python. You might also like this article on list comprehension in python.

Related

Recommended Python Training

Course: Python 3 For Beginners

Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.

Enroll Now

Filed Under: Basics Author: Aditya Raj

More Python Topics

API Argv Basics Beautiful Soup Cheatsheet Code Code Snippets Command Line Comments Concatenation crawler Data Structures Data Types deque Development Dictionary Dictionary Data Structure In Python Error Handling Exceptions Filehandling Files Functions Games GUI Json Lists Loops Mechanzie Modules Modules In Python Mysql OS pip Pyspark Python Python On The Web Python Strings Queue Requests Scraping Scripts Split Strings System & OS urllib2

Primary Sidebar

Menu

  • Basics
  • Cheatsheet
  • Code Snippets
  • Development
  • Dictionary
  • Error Handling
  • Lists
  • Loops
  • Modules
  • Scripts
  • Strings
  • System & OS
  • Web

Get Our Free Guide To Learning Python

Most Popular Content

  • Reading and Writing Files in Python
  • Python Dictionary – How To Create Dictionaries In Python
  • How to use Split in Python
  • Python String Concatenation and Formatting
  • List Comprehension in Python
  • How to Use sys.argv in Python?
  • How to use comments in Python
  • Try and Except in Python

Recent Posts

  • Count Rows With Null Values in PySpark
  • PySpark OrderBy One or Multiple Columns
  • Select Rows with Null values in PySpark
  • PySpark Count Distinct Values in One or Multiple Columns
  • PySpark Filter Rows in a DataFrame by Condition

Copyright © 2012–2025 · PythonForBeginners.com

  • Home
  • Contact Us
  • Privacy Policy
  • Write For Us