Append Files in Python

An Overview of File Handling in Python

Handling files in Python helps the programmers read and write files. Python provides various functions to handle files. Generally, there are two types of files: Text files and Binary files.

Basic Terminologies and Important Concepts of File Handling

  • Open( ): This function is used to open a file. We must open the file before performing any operation on it. The file can be opened using different modes.
  • r: This is the read mode, where we can open and read an existing file.
  • w: This is the write mode used to write data in a file. The write mode will override the data in the existing file or replace the new data with the old data. It will create a new file if no file exists.
  • a: This is the append mode, where it opens the file, and we can write the data into the file at the end.
  • r+: This is the read-and-write mode, where we can add the data and override the previous one.
  • w+: This is the write-and-read mode, where we can replace the existing data and add a new one.
  • a+: This is append-and-read mode. This will append the data without overriding the data and read it.
  • Close( ): This function is used to close a file. After opening and performing operations on it, it is necessary to close the file.

We will understand different functions and methods to append the file in Python.

Opening a file in append mode shifts the pointer to the end of the file and inserts the data just after the existing data.

Let's understand the concept of append files using some examples in Python.

The given text file is the input file to which we will append the data.

Append Files in Python

Program 1: Open an Already Existing File in Append Mode

Output:

hello, 
this is a demo file.OK

Append Files in Python

We have created a text file (demo.txt) and added some content.

We created an object to handle the file. Using the open( ) function, we opened an existing text file in append mode ('a'). Then, we have added some data using the write( ) function. Then, we closed the file using the close( ) function. Again, we opened the file in read mode and printed the file's content using the read( ) function. It will print the data just after the existing data of the file.

The append mode will override the data, and the changes will also be visible in the original file.

Program 2: Appends Data in the File in a New Line

When we append a file, it adds the data just after the last data in the file but not in the next line. We can append the data in the next line by adding \n in the file.write( ) function. We can open the file with append or write mode. It will move the pointer to the end of the file. We will append the newline character (\n), adding the data to the new line.

Output:

hello, 
this is a demo file.
OK
Adding a new Line

Append Files in Python

Program 3: Append the File using a+ Mode.

The Append and Read mode (a+) is used to open the file, append it, and then read it.

Output:

hello, 
this is a demo file.OK

Adding a new Line
This is append mode

Append Files in Python

We opened the text file (demo.txt) in a+ mode, by which the pointer moves to the end of the file. The write( ) function was used to add the data to the file and then printed it using the read( ) function. There is no need to open a file two times in append and read mode. Both read and write functions can be used in append and read mode (a+).

Program 4: Using with Statement to Append the File

The file can also be opened using the with statement. Using with statement, we don't need to close the file using the close( ) function. It automatically closes the file after its execution.

Output:

hello, 
this is a demo file.OK

Adding a new Line
This is append mode Append the file using with statement

Append Files in Python

We can also execute this program without closing the file:

Program 5: Adding Multiple Lines in the File

Output:

hello, 
this is a demo file.OK

Adding a new Line
This is append mode Append the file using with statement

New Line
Adding Multiple Lines in the file

Append Files in Python

We have opened the text file (demo.txt) in a+ mode (append and write mode), declared a variable 'lines', and defined a list of content we want to append. Then, using the writelines( ) function, we write the content. We can pass any list, tuple, or iterable to append the file. We can add multiple lines in just one line by defining a list or tuple.

Program 6: Create a New File and Append it.

We can create a new file using the write mode and can append the file using the append mode.

Output:

Creating a new file
In write mode...
Adding data in a new file
In append mode...

Append Files in Python

We created a new text file (text.txt) by opening the file in write mode. Then, added the data using the writelines( ) function. We closed the file using the close( ) function. Now, we opened the file using append mode, which will take the pointer at the end of the file and appended the file using the writelines( ) function.