Open In App

Python – Copy contents of one file to another file

Last Updated : 22 Nov, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

Given two text files, the task is to write a Python program to copy contents of the first file into the second file.

The text files which are going to be used are second.txt and first.txt:

Method #1: Using File handling to read and append

We will open first.txt in ‘r’ mode and will read the contents of first.txt. After that, we will open second.txt in ‘a’ mode and will append the content of first.txt into second.txt.

Example:

Python3




# open both files
with open('first.txt','r') as firstfile, open('second.txt','a') as secondfile:
      
    # read content from first file
    for line in firstfile:
               
             # append content to second file
             secondfile.write(line)


Output:

Method #2: Using File handling to read and write

We will open first.txt in ‘r’ mode and will read the contents of first.txt. After that, we will open second.txt in ‘w’ mode and will write the content of first.txt into second.txt.

Example:

Python3




# open both files
with open('first.txt','r') as firstfile, open('second.txt','w') as secondfile:
      
    # read content from first file
    for line in firstfile:
               
             # write content to second file
             secondfile.write(line)


Output:

Method #3: Using shutil.copy() module

The shutil.copy() method in Python is used to copy the content of the source file to destination file or directory. 

Example:

Python3




# import module
import shutil
  
# use copyfile()
shutil.copyfile('first.txt','second.txt')


Output:



Similar Reads

Python - Copy all the content of one file to another file in uppercase
In this article, we are going to write a Python program to copy all the content of one file to another file in uppercase. In order to solve this problem, let's see the definition of some important functions which will be used: open() - It is used to open a file in various modes like reading, write, append, both read and write.write() - It is used t
2 min read
copy in Python (Deep Copy and Shallow Copy)
In Python, Assignment statements do not copy objects, they create bindings between a target and an object. When we use the = operator, It only creates a new variable that shares the reference of the original object. In order to create "real copies" or "clones" of these objects, we can use the copy module in Python. Syntax of Python DeepcopySyntax:
5 min read
Python | How to copy data from one excel sheet to another
In this article, we will learn how to copy data from one excel sheet to destination excel workbook using openpyxl module in Python. For working with excel files, we require openpyxl, which is a Python library that is used for reading, writing and modifying excel (with extension xlsx/xlsm/xltx/xltm) files. It can be installed using the following com
3 min read
Copy all files from one directory to another using Python
In this article, we will discuss how to copy all files from one directory to another using Python. This can be done using the shutil module. This module can be used in Python to perform operations on files and folders in a directory. Shutil package facilitates the access, movement, and removal of the files between directories. Method 1 : Using shut
2 min read
How Can I Make One Python File Run Another File?
In Python programming, there often arises the need to execute one Python file from within another. This could be for modularity, reusability, or simply for the sake of organization. In this article, we will explore different approaches to achieve this task, each with its advantages and use cases. Making One Python File Run Another File in PythonBel
2 min read
Read content from one file and write it into another file
Prerequisite: Reading and Writing to text files in Python Python provides inbuilt functions for creating, writing, and reading files. Two types of files can be handled in python, normal text files and binary files (written in binary language,0s, and 1s). Text files: In this type of file, Each line of text is terminated with a special character call
2 min read
Copy a Python Dictionary and only Edit the Copy
Copying a dictionary in Python to preserve the original while making changes to the duplicate is a common task. Developers use methods like the `copy` module or the dictionary's `copy()` method to create an independent duplicate, allowing for isolated edits. This article explores commonly used techniques to copy a dictionary and modify only the dup
4 min read
Python Dictionary Copy Vs Deep Copy
In Python, Dictionary like other non-primitive data structures can be copied using various methods. The key distinction lies in the depth of the copy. In this article, we are going to learn about deep copy and shallow copy in Python. What is Shallow Copy in Python Dictionary?In Python, a shallow copy creates a new object that references the same un
3 min read
Python - Append content of one text file to another
Having two file names entered by users, the task is to append the content of the second file to the content of the first file with Python. Append the content of one text file to anotherUsing file objectUsing shutil moduleUsing fileinput moduleSuppose the text files file1.txt and file2.txt contain the following data. file1.txt file2.txt Append the c
3 min read
How to run multiple Python file in a folder one after another?
In this article, we will discuss how to run multiple python files in a folder one after another. There can be many ways to this task, here, we will discuss a few of them. For doing this program, we have to create some python files in one folder and give some names to that folder. The content inside a.py file: print("a") The content inside b.py file
3 min read