Open In App

How to keep old content when Writing to Files in Python?

Last Updated : 02 Feb, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we are going to discuss various approaches to keep old content while writing files in Python.

We can keep old content while using write in python by opening the file in append mode. To open a file in append mode, we can use either ‘a‘ or ‘a+‘ as the access mode. The definition of these access modes are as follows:

  • Append Only (‘a’): Open the file for writing. The file is made if it doesn’t exist. The handle is positioned at the top of the file. The data being written are going to be inserted at the top, after the prevailing data.
  • Append with Read (‘a+’): Open the file for reading and writing. The file is made if it doesn’t exist. The handle is positioned at the top of the file. The data being written are going to be inserted at the top, after the prevailing data.

Approach:

  • We will first open the file in the append mode i.e. make use of either ‘a’ or ‘a+’ access mode to open a file.
  • Now, we will simply add the content at the bottom of the file thus keeping the old content of the file.
  • Then, we will close the opened file in the program.

We are going to use the below text file to perform all the approaches:

Below is the complete implementation of the approach explained above:

Example1: Adding new content to the file while keeping the old content with ‘a’ as an access mode.

Python3




# Python program to keep the old content of the file
# when using write.
  
# Opening the file with append mode
file = open("gfg input file.txt", "a")
  
# Content to be added
content = "\n\n# This Content is added through the program #"
  
# Writing the file
file.write(content)
  
# Closing the opened file
file.close()


Output:

Example 2: Adding new content to the file while keeping the old content with ‘a+’ as an access mode.

Python3




# Python program to keep the old content of the file
# when using write.
  
# Opening the file with append mode
file = open("gfg input file.txt", "a+")
  
# reach at first
file.seek(0)
  
# Reading the file
content = file.read()
  
# view file content
print(content)
  
# Content to be added
content = "\n\n# This Content is added through the program #"
  
# Writing the file
file.write(content)
  
# Closing the opened file
file.close()


Output:

A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes etc.



Previous Article
Next Article

Similar Reads

How to Download Python Old Version and Install
Python is a popular programming language known for its simplicity and versatility. When we install Python in our systems we always install the latest version of it but there may be situations where we need to install an older version of Python due to compatibility reasons or to work on legacy projects so that we can use old features of Python that
2 min read
Comparing Old-Style and New-Style Classes in Python
In Python, the difference between old-style and new-style classes is based on the inheritance from the built-in object class. This distinction was introduced in Python 2.x and was fully adopted in Python 3.x, where all classes are new-style classes. In this article, we will see the difference between the old-style and new-style classes in Python. P
4 min read
Writing files in background in Python
How to write files in the background in Python? The idea is to use multi-threading in Python. It allows us to write files in the background while working on another operation. In this article, we will make a 'Asyncwrite.py' named file to demonstrate it. This program adds two numbers while it will also write a file in the background. Please run this
2 min read
Reading and Writing XML Files in Python
Extensible Markup Language, commonly known as XML is a language designed specifically to be easy to interpret by both humans and computers altogether. The language defines a set of rules used to encode a document in a specific format. In this article, methods have been described to read and write XML files in python. Note: In general, the process o
6 min read
Reading and Writing CSV Files in Python
CSV (Comma Separated Values) format is the most common import and export format for spreadsheets and databases. It is one of the most common methods for exchanging data between applications and popular data format used in Data Science. It is supported by a wide range of applications. A CSV file stores tabular data in which each data field is separa
4 min read
Writing CSV files in Python
CSV (Comma Separated Values) is a simple file format used to store tabular data, such as a spreadsheet or database. CSV file stores tabular data (numbers and text) in plain text. Each line of the file is a data record. Each record consists of one or more fields, separated by commas. The use of the comma as a field separator is the source of the nam
6 min read
Reading and Writing to text files in Python
Python provides built-in functions for creating, writing, and reading files. Two types of files can be handled in Python, normal text files and binary files (written in binary language, 0s, and 1s). Text files: In this type of file, Each line of text is terminated with a special character called EOL (End of Line), which is the new line character ('
9 min read
How to merge multiple excel files into a single files with Python ?
Normally, we're working with Excel files, and we surely have come across a scenario where we need to merge multiple Excel files into one. The traditional method has always been using a VBA code inside excel which does the job but is a multi-step process and is not so easy to understand. Another method is manually copying long Excel files into one w
4 min read
Python - Reverse a words in a line and keep the special characters untouched
Reverse the characters in all words in a line including numbers, but leave special characters and symbols untouched in the same position. Consider the below examples. Input: 'Bangalore is@#$!123 locked again in jul2020' should change to Output: 'erolagnaB si@#$!321 dekcol niaga ni 0202luj' Input: 'Bangalore is@#$!123locked locked again in jul2020'
3 min read
How to keep your PC awake automatically using Python?
In this article, we are going to discuss how to keep your PC awake using Python. In order to perform this task, we are going to use an external Python module. Here we will be using a python package PyAutoGUI (cross-platform GUI automation Python module used to programmatically control the mouse & keyboard. ) This will keep your PC awake forever
2 min read