HomePythonPython Program to Append the Content of One File to the End of Another File

Python Program to Append the Content of One File to the End of Another File

Text files often contain various characters, including blank spaces. This python program demonstrates how to create a Python program that reads a text file and counts the number of blank spaces present in its contents.

Problem Statement

Write a Python program that takes the names of two text files as input, reads the content of one file, and appends that content to the end of another file.

Python Program to Append the Content of One File to the End of Another File

def append_file_contents(source_file, target_file):
    try:
        with open(source_file, 'r') as source:
            source_contents = source.read()
            with open(target_file, 'a') as target:
                target.write(source_contents)
        return True
    except FileNotFoundError:
        return False

# Input
source_file_name = input("Enter the name of the source file: ")
target_file_name = input("Enter the name of the target file: ")

# Appending content to the target file
success = append_file_contents(source_file_name, target_file_name)

# Output
if success:
    print("Content appended successfully.")
else:
    print("Source file not found. Content could not be appended.")

Input / Output

Python Program to Append the Content of One File to the End of Another File

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

In this Python program, we will create a singly linked list and remove duplicate elements from it. A linked list...
This Python program solves the Celebrity Problem by finding a person who is known by everyone but does not know...
This Python program uses a recursive approach to solve the n-Queens problem. It explores all possible combinations of queen placements...