Python Program to Append a String or Text to an Existing File


In Python, we have file handling in-built functions like open(), write(), and close() that can be used to Append a String or Text to an Existing File. The term append is used in the program of file handling in Python. To append the text or string means that the user writes the new text to the existing file. The main purpose of adding text to an existing file means giving extra information to the existing file. Note-taking and Data collection are such applications of this program.

Syntax

The following syntax is used in the example is −

open("file_name.txt", mode)

The open method is used for opening a file in Python. This accepts the two parameters −

  • file_name.txt − Mention the file name.

  • mode − This parameter determine the file positioned and what the method allows.

write("write some text")

The write method is used to write something to the file in Python.

close()

The close method closes the opening file in Python.

Example 1

In the following example, we will first initialize the variable named f which will initialize the value by opening the file. Then the open method accepts two parameters- mydoc.txt(To mention the name of a text file) and a(This is mode ‘a’ appending the text to the existing file). Then start the write() method with a variable named f to write some text which will be added to the existing text file. Next, close the opening file by using close() and print the statement that “The text is appended to file”.

# append the text to an existing file
f = open( "mydoc.txt", "a")
f.write("\nThis is Python 3.0")
f.close()
print("The text is appended to file")

Output

The text is appended to file

Example 2

In the following example, we will start the program by using ‘with open()’ method to open the text file. This method accepts two parameters i.e. ‘mydoc.txt’(opening the file) and ‘a’(append text to existing file). Then use the write() method to write some text which will add the text to the existing text file. Next, print the statement as “The text is appended to file”.

# append the text to an existing file
with open('mydoc.txt', 'a') as file:
   file.write('\nThis is new method.')
print("The text is appended to file")

Output

The text is appended to file

Example 3

In the following example, the function append_file is defined in this code, and it adds text to a given file. Using a with statement, it opens the file in "a" (append) mode and appends the text to the end of the file. Then, the function is called with the arguments for the file name and the text to append.

def append_file(file_name, text):
   with open(file_name, 'a') as file:
      file.write(text)
append_file('mydoc.txt', 'Append text')
print("The text is appended to file")

Output

The text is appended to file

Conclusion

We observe the differences between two examples and find the similarity between them. Both examples use the mode ‘a’ that appended the text to an existing file and also used the open and write for opening and writing to the file respectively. Therefore, the written text is appended to an existing file.

Updated on: 01-Jun-2023

461 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements