Program to Copy the Contents of One File into Another

Python Program to Copy the Contents of One File into Another

Are you new to the java programming language? We recommend you to ace up your practice session with these Basic Java Programs Examples

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 two files, the task is to copy the contents of one file to the other file.

Program to Copy the Contents of One File into Another

Below is the full process to copy 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.
  • In read mode, open the first file say  samplefile1.txt.
  • In write mode, open the second file say  samplefile2.txt.
  • Using for loop, go over the lines in the first file.
  • Copy the ith line of the first file to the second file using the write function.
  • All the content from the first file will be copied to the second file.
  • The exit of the program.

Below is the implementation:

# In read mode, open the first file say  samplefile1.txt.
with open("samplefile1.txt") as file1:
  # In write mode, open the second file say  samplefile2.txt.
    with open("samplefile2.txt", "w") as file2:
      # Using for loop, go over the lines in the first file.
        for iline in file1:
          # Copy the ith line of the first file to the second file using the write function.
            file2.write(iline)

Explanation:

  • The f stream is used to open the file samplefile1.txt with the open() function.
  • Another file, samplefile2.txt, is opened in write mode using the f1 stream using the open() function.
  • A for loop is used to iterate over each line in the file (in the input stream).
  • The output file contains each of the iterated lines.

Output:

Before Copying:

Samplefile1.txt

Do you Love to Program in Python Language? Are you completely new to 
the Python programming language? Then, refer to this ultimate guide on
Python Programming and become the top programmer. For detailed information
such as What is Python? Why we use it? Tips to Learn Python Programming Language,
Applications for Python dive into this article.

Samplefile2.txt

This is BtechGeeks

After Copying:

Samplefile1.txt

Do you Love to Program in Python Language? Are you completely new to 
the Python programming language? Then, refer to this ultimate guide on
Python Programming and become the top programmer. For detailed information
such as What is Python? Why we use it? Tips to Learn Python Programming Language,
Applications for Python dive into this article.

Samplefile2.txt

Do you Love to Program in Python Language? Are you completely new to 
the Python programming language? Then, refer to this ultimate guide on
Python Programming and become the top programmer. For detailed information
such as What is Python? Why we use it? Tips to Learn Python Programming Language,
Applications for Python dive into this article.

Google Colab Images:

Files and Code:

Samplefile2.txt:

Samplefile1.txt:

Related Programs: