Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages

Copy a File in Python

Copying A File In Python2

In our previous tutorials, we learned some Python file operations like reading, writing and deleting. Let’s learn to copy a file in Python in this tutorial.

We can copy a file in Python using different methods under the below-mentioned modules,

  • shutil module
  • os module
  • subprocess module

In this tutorial, we are going to learn using the different methods provided by the above modules to copy a file in Python.

1. shutil Module to Copy a File in Python

The shutil module provides some easy to use methods using which we can remove as well as copy a file in Python. Let’s look at the different methods defined under this module specifically used for copying.

1. copyfileobj()

The copyfileobj() method copies the content of the source file to the target file using their respective file objects. Let’s take a look at the code below,

import shutil
src_file_obj=open('src.txt', 'rb')
targ_file_obj= open('targ.txt' , 'wb')
shutil.copyfileobj( src_file_obj , targ_file_obj )

Note: that the file objects should be pointing to the 0 positions (start position) for both the respective source and target files, to copy the entire content.

2. copyfile()

The copyfile() method copies the content from the source to the target file using the file paths. It returns the target file path. The target file path must be writeable or else an OSerror exception would occur.

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

It is to be kept in mind, that the method only allows the use of file paths and not directories.

3. copy()

This method copies the source file to the target file or the target directory. Unlike copyfile(), the method copy() allows the use of the target directory as an argument and also copies the file permissions. copy() returns the path to the target file after copying the contents.

import shutil
shutil.copy('/Users/test/file.txt', '/Users/target/')

A file named ‘file.txt’ is created in the target destination with all the content and permissions copied from ‘/Users/test/file.txt’.

4. copy2()

The copy2() method is used exactly the same way as of the copy() method. They also function in the same way, except for the fact that copy2() also copies the meta-data from the source file.

import shutil
shutil.copy2('/Users/test/file.txt', '/Users/target/')

2. os Module to Copy a File in Python

1. popen()

The popen() method creates a pipe to the command, cmd. The method returns a file object connected to the cmd pipe. Take a look at the code below,

#for Windows
import os
os.popen('copy src.txt targ.txt' )
#for Linux
import os
os.popen('cp src.txt targ.txt' )

With this method, not only can we copy files but also execute other regular commands.

2. system()

The system() method directly calls and executes a command argument in a subshell. Its return value depends on the OS that runs the program. For Linux, it is the exit status, whereas for Windows it is the return value by the system shell.

#for Linux
import os
os.system(' cp src.txt targ.txt' )
#for Windows
import os
os.system(' copy src.txt targ.txt' )

3. subprocess Module to Copy a File in Python

1. call()

The call() method similar to os.system() directly calls or runs the command passed as an argument to the function.

# In Linux
import subprocess'cp source.txt target.txt', shell=True)
# In Windows
import subprocess'copy source.txt target.txt', shell=True)