Register Login

How to Copy File in Python

Copying a file from one folder to another is a fundamental skill in working with different Operating system operations. Many applications require copying files from one location to another. With the help of a python script, it is very easy to perform this. In software development and data science, there are many scenarios where we need to have a backup of a file.

With two or three lines of code, a backup of a database file or any other file can be created from within a Python application. This article will give you a complete overview of how Python programmers can write simple codes snippets to copy files in Python.

Different Module to copy files in python:

There are many inbuilt modules in python to copy files from one directory to another. Some of them are as follows

  1. Shuttle module
  2. OS module
  3. Subprocess module

Shutil module:

There are many methods in shutil to copy files. Some of them are as follows

  1. Shutil. copy,
  2. Shutil.copyfile
  3. Shutil.copy2
  4. Shutil.copyfileobj
  5. Shutil.copytree

These modules differ in the way they preserve permissions or not, supporting Directory or not, supporting file objects or not.

Shutil.copy:

The copy method copies a file to a destination file without metadata. The method returns the directory of the new file created.

Syntax:

shutil.copy(src, dst, *, follow_symlinks=True)

Program:

import shutil
shutil.copy('file.txt', 'new_file.txt')

Explanation:

First import the shutil module. With the shutil module call the copy method which takes two arguments as the source and destination files location which are file.txt and new_file.txt.

Shutil.copyfile:

The copyfile method copied a file from source to destination without metadata. The destination can not be a directory.

Syntax:

shutil.copyfile(src, dst, *, follow_symlinks = True)

Program:

import shutil
shutil.copyfile('src.txt', 'des.txt')

Explanation:

First import the shutil module. With the shutil module call the copyfile method which takes two arguments as the source and destination files location which are file.txt and new_file.txt.

Shuttle.copy2:

Copy2 method copies a file from source to destination with metadata.

Syntax:

shutil.copy2(src, dst, *, follow_symlinks=True)

Program:

import shutil
shutil.copy2('file.txt', 'new_file.txt')

Explanation:

First import the shutil module. With the shutil module call the copy2 method which takes two arguments as the source and destination files location which are file.txt and new_file.txt.

Shutil.copyfileobj:

The Copyfileobj method is used to copy the source file object to the destination file object.

Syntax:

shutil.copyfileobj(fsrc, fdst[, length])

Program:

import shutil
src = open('file.txt', 'rb')
dest= open('file_copy.txt', 'wb')
shutil.copyfileobj(src, dest)

Explanation:

First import the shutil module. With the shutil module call the copyfileobj method which takes two arguments as the source and destination files location which are file.txt and new_file.txt.

Shutil.copytree:

The copy tree method recursively copies a full directory tree of the source to the destination directory. It returns the destination directory.

Syntax:

shutil.copytree(src, dst, symlinks = False, ignore = None, copy_function = copy2, igonre_dangling_symlinks = False)

Program:

import shutil
src = 'C:/Users / folder1'
dest = 'C:/Users / folder2'
new_dest = shutil.copytree(src, dest)

Explanation:

First import the shutil module. With the shutil module call the copytree method which takes two arguments as the source directory and destination directory location which are src and dest.

Method Preserves Permissions Supports Directory Copies Metadata Supports ile object
copy() Yes Yes No No
copyfile() No No No No
copy2() Yes Yes Yes

No

copyfileobj() No No No Yes

os module:

There are many methods in the os module to copy files. Some of them are as follows

  1. Os.popen
  2. Os.system

os.popen:

The Popen method opens a pipe in  cmd. The return value is an open file object connected to the pipe. Which can be used to copy files from the source to the destinations folder.

Program:

import os
os.popen(cmd[, mode[, bufsize]])
os.popen('copy src.txt dest.txt')

Explanation:

First import the os module. With the os, the module calls the Popen to open a pipe to cmd. Again call the Popen method with the source and destination file location to copy.

os.system:

The system method executes the command in a subshell. Its return value is that returned by the system shell after running the command which can be used to copy files from one folder to another.

Program:

os.system(command)
os.system('copy src.txt dest.txt')

Explanation:

First import the os module. With the os module open the cmd then call the system to copy the file from source to destination.

Subprocess module:

There are many methods in the subprocess module to copy files. Some of them are as follows

  1. Subprocess.call
  2. Subprocess.check_output

Subprocess.call:

Call method run command with arguments. If the value returned by the method is zero then return, otherwise raises a  called process error. Files can be copied bypassing the source and destination inside the call method and sett

Program:

subprocess.call(args, *, stdin = None, stdout = None, stderr = None, shell = False)
status = subprocess.call('copy source.txt destination.txt', shell = True)

subprocess.check_output:

Check_Output runs the command with arguments. If a nonzero value is returned by the method call then it raises a calledprocess error. Files can be copied bypassing the source and destination inside the check_output method and setting shell equal to true.

Program:

subprocess.check_output(args, *, stdin = None, stderr = None, shell = False, universal_newlines = False)
status = subprocess.check_output('copy source.txt destination.txt', shell = True)

Conclusion:

We learn about different methods to copy files in python by Shutil module (shutil.copy, Shutil.copyfile, Shutil.copy2, Shutil.copyfileobj, Shutil.copytree), os module(os.popen signature, os.system signature), subprocess module,(subprocess.call, subprocess.check_output). Copying a file or a large collection of files (database) is so easy with python.

With two or three lines of code, the file can be copied to any location. Among all the three modules, the os module is the most frequently used and is efficient compared to others.