How to copy file to another directory in Python

Python copy file to another directory

In this article, we will see different ways to copy file to another directory in Python.

We can read and write files in Python. We can also work with paths and directories using different libraries.

Ways to copy file to another directoy in Python

We will discuss different methods to copy file from one destination to another.

Using file handling

This method is useful for only small files. We will use a file object to read from a file, and write this to another file.

For example,

In the above example,

  • The open() function returns a file object by opening the file at the given path.
  • The read() function reads the content from the file.
  • The write() function writes the data to the destination file.

Using the shutil library

The shutil library has many functionalities to perform various operations on files and paths. We can use several methods from this library to copy file to another directory in Python.

First, we have the shutil.copy() function. It creates a copy of the given source file in the provided destination. It accepts two parameters src and dst. Both parameters are assumed to be path-like strings. The destination (dst) specified can also be a target directory. In that case, the original filename is provided to the copy. It preserves the permissions associated with the file.

For example,

There is also the shutil.copy2() function. It performs the same function as the shutil.copy() method but also preserves the metadata associated with the file.

For example,

The shutil.copyfile() function creates a copy of the given file, but it does not preserve the permissions or the metadata of the file. The destination cannot only be a directory and needs to have the filename and the path.

For example,

The shutil.copyfileobj() function uses the file handling objects to create copies of the file. It copies the content from the source file object to the destination file object.

For example,

In the above example,

  • We open the file which we want to copy using the open() function in read mode and create the file object f1.
  • We create the f2 file object using the open() function in write mode and open the file where we want to copy.
  • We copy the contents using the file objects in the copyfileobj() function.
  • We close both the file objects using the close() function.

In all the above methods, we get the SameFileError error if both the source and destination are the same file. It also raises the IOError if the destination is not writable.

Using the pathlib library

This method works for Python 3.5 and above, and is similar to the file handling method. Using the pathlib library, we create two Path objects with the path of the source file and of the destination file.

Then, we will use the read_bytes() function to read data from source and write this to the destination file using the write_bytes() function.

For example,

Using the os module

The os module provides a wide range of functionalities to interact and work with the operating system.

The os.popen() function returns a file object, connected to a pipe. We can run commands using this. For windows, we can use the copy command and specify the source and destination file to create a copy. The copy command is used in the command line to create copies of a file. This function will execute this command using Python.

For example,

Similarly, for Linux users, we can use the cp command in place of copy.

We can also execute this command in a subshell using the os.system() function. It will run the command, creating the required copies.

For example,

Using the subprocess module

Similar to the os module, we can use different functions from the subprocess module to run the copy command to copy file to another directory in Python.

The subprocess.call() function runs the specified command and returns the child return code. We can use it to copy files.

See the code below.

Similarly, we can use the subprocess.check_output() function to run the copy command and view the output of this command.

For example,

Conclusion

In this tutorial, we discussed different methods from the shutil, os, and subprocess modules to create copies of a file. The os and subprocess modules use the copy command in different functions to create a copy of the file. The shutil library provides direct functions to create copies of a file and does not use any other command-line command.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *