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.


import_contacts

You may also like:

Related Posts

  • Copy file Python
    12 January

    How to Copy File in Python?

    Table of ContentsCopy file using the shutil module in PythonCopy file using the copyfileobj() function in PythonCopy file using the copyfile() function in PythonCopy file using the copy() function in PythonCopy file using the copy2() function in PythonCopy file using the operating system command in PythonCopy file using the os module in PythonCopy file using […]

  • Python absolute path
    17 November

    Get Absolute Path in Python

    Table of ContentsWhat is an absolute path to a file? How to get absolute path in PythonUse abspath to get the absolute path to file use pathlib module to get the absolute path to file Conclusion We often use files to store the outputs of our programs in permanent storage. Generally, we work with relative […]

  • Python create file if not exists
    06 November

    Create file if not exists in Python

    Table of ContentsCreating Files in PythonWays to create file if not exists in PythonUsing the open() functionUsing the pathlib.Path.touch() functionUsing the os.path.exists() functionUsing the try and except blockConclusion Creating Files in Python File handling is a handy concept in programming. We can write and read data to files. In Python, to create or open a […]

  • Python overwrite file
    06 November

    Overwrite file in Python

    Table of ContentsFile Handling in PythonWays to overwrite file in PythonUsing the w parameter in the open() functionUsing the file.truncate() functionUsing the replace() functionUsing the pathlib moduleUsing the os.remove() function File Handling in Python We can read and write content to files in Python. We use the open() function to create files and specify the […]

  • Iterate through files in directory in Python
    30 August

    Iterate through files in directory in python

    Table of ContentsUsing the os.scandir() function to iterate through files in a given directory in Python.Using the os.listdir() function to iterate through files in a given directory in Python.Using the os.walk() function to iterate through files in a given directory in Python.Using the pathlib module to iterate through files in a given directory in Python.Using […]

  • Write a list to a file in Python
    12 June

    Write a list to a file in python

    Table of ContentsUsing the write() function to write a list to a file in PythonUsing the writelines() function to write a list to a file in PythonUsing the pickle.dump() function to write a list to a file in PythonUsing the json.dump() function to write a list to a file in PythonUsing the unpack operator * […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.