Program to Append the Contents of One File to Another File

Python Program to Append the Contents of One File to Another File

If you are new to Java and want to learn the java coding skills too fast. Try practicing the core java programs with the help of the Java basic programs list available.

Files in Python:

A file is a piece of information or data that is kept in computer storage devices. You are already familiar with several types of files, such as music files, video files, and text files. Python makes it simple to manipulate these files. In general, files are classified into two types: text files and binary files. Text files are plain text, whereas binary files include binary data that can only be read by a computer.

Python file handling (also known as File I/O) is an important topic for programmers and automation testers. It is necessary to work with files in order to either write to or read data from them.

Furthermore, if you are not already aware, I/O operations are the most expensive procedures through which a program might fail. As a result, you should take caution while implementing file handling for reporting or any other reason. Optimizing a single file activity can contribute to the development of a high-performance application or a solid solution for automated software testing.

Program to Append the Contents of One File to Another File

Below is the full process to append the contents of one file to another file.

Approach:

  • Create the first file or upload the existing file.
  • Create the second file or upload the existing file.
  • Enter the file name of the first file using the input() function and store it in a variable.
  • In read mode, open the first file with the entered first file name.
  • Enter the file name of the second file using the input() function and store it in another variable.
  • In append mode, open the second file with the above-entered second file name.
  • Read the first file using the read() function and store it in a variable.
  • Close the first file using close() function.
  • Append the contents of the first file using the write() function.
  • Close the second file using close() function.
  • The exit of the program.

Below is the implementation:

# Enter the file name of the first file using the input() function and store it in a variable.
filename1 = input("Enter the first file name = ")
# Enter the file name of the second file using the input() function and store it in another variable.
filename2 = input("Enter the second file name = ")
# In read mode, open the first file with the entered file name.
file1 = open(filename1, "r")
# Read the first file using the read() function and store it in a variable.
filedata1 = file1.read()
# Close the first file using close() function.
file1.close()
# In append mode, open the second file with the above-entered second file name.
file2 = open(filename2, "a")
# Append the contents of the first file using the write() function.
file2.write(filedata1)
file2.close()

Explanation:

  • The user must provide the file to be read from and the file to append to.
  • In read mode, the file to be read is opened with the open() function.
  • The contents of the file read with the read() function are saved in a variable, and the file is then closed.
  • The file to which the data will be appended is opened in append mode, and the data stored in the variable is written into it.
  • After then, the second file is closed.

Output:

Before Appending:

Samplefile1.txt:

By now you might be aware that Python is a Popular Programming Language used 
right from web developers to data scientists. Wondering what exactly Python looks
like and how it works? The best way to learn the language is by practicing. We have listed a wide 
collection of Python Programming Examples.You can take references 
from these examples and try them on your own.

Samplefile2.txt:

hello this is btechgeeks

After Appending:

Samplefile1.txt:

By now you might be aware that Python is a Popular Programming Language used 
right from web developers to data scientists. Wondering what exactly Python looks
like and how it works? The best way to learn the language is by practicing. We have listed a wide 
collection of Python Programming Examples.You can take references 
from these examples and try them on your own.

Samplefile2.txt:

hello this is btechgeeksBy now you might be aware that Python is a Popular Programming Language used 
right from web developers to data scientists. Wondering what exactly Python looks
like and how it works? The best way to learn the language is by practicing. We have listed a wide 
collection of Python Programming Examples.You can take references 
from these examples and try them on your own.

Google Colab Images:

Files and Code:

Samplefile2.txt:

Samplefile1.txt:

Files in Python:

Python, like many other programming languages, offers file handling and allows users to read and write files, as well as perform a variety of other file-related tasks. The concept of file handling has been extended to a variety of other languages, but the implementation is either complicated or lengthy. However, like most Python principles, this concept is simple and straightforward. Python processes file differently depending on whether they are text or binary, which is crucial. Each line of code consists of a series of characters that together constitute a text file. A specific character called the EOL or End of Line character, such as the comma, or a newline character is used to end each line in a file.

Given a file, the task is to read the given text file and print all the numbers present in the given text file.

Program to Read a Text File and Print all the Numbers Present in the Text File

Below is the full process to read a text file and print all the numbers present in the given text file.

Approach:

  • Create the sample file or upload the existing file.
  • In read mode, open the first file say  samplefile.txt.
  • Using for loop, go over the lines in the sample file.
  • Split the line into words using the split() function.
  • Traverse through the words using for loop.
  • Traverse through all the characters of the word using another For loop.
  • Check to see if the letter is a digit, and if so, print it.
  • Exit of program

Below is the implementation:

filename = input('Enter the name of the given file : ')
# In read mode, open the given file with the name 'filename'
with open(filename, 'r') as file:
  # Using for loop, go over the lines in the sample file.
    for line in file:
      # Split the line into words using the split() function.
        words = line.split()
        # Traverse through the words using for loop.
        for i in words:
          # Traverse through all the characters of the word using another for loop.
            for letter in i:
              # Check to see if the letter is a digit, and if so, print it.
                if(letter.isdigit()):
                    print(letter)

Output:

Enter the name of the given file : samplefile.txt 
 1
 1
 2
 3
 9
 4
 5
 6
 7
 9
 8
 3
 3

Explanation:

  • A file name must be entered by the user.
  • In read mode, the file is opened with the open() method.
  • To read each line in the file, a for loop is utilized.
  • Using split, each line is divided into a list of words ().
  • The word list is traversed using a for loop, and the letters of the word are traversed using another For loop.
  • The digit is printed if the letter encountered is a digit.
  • Here it prints all the digits present in the given file separately in a newline.

Sample file.txt :

hel11l2o th3is i9s B4TechGe5eks Onl6ine Cod7ing Platf9orm fo8r Bt3ech Stude3nts

Google Colab Images:

File and Code:

 

Output Image:

 

File Content:

Related Programs: