Built with Tutify.io

    AIStudyLabs

    How to Copy Files in Python

    How to Copy Files in Python

    How to copy files in Python? Python provides a variety of ways to work with files, including copying them. In this article, we will explore the different methods for copying files in Python with examples. It's essential to choose the right function depending on the requirements of the task at hand.

    In some cases, it is necessary to preserve the original file's metadata, such as file permissions and timestamps, while in other cases it may not be necessary. Python provides different functions for copying files that allow for preserving or ignoring metadata.

    <a id="h-1-quick-examples"></a>

    1. Quick Examples

    The following examples will give you a high-level overview of different methods to copy files in python.

    
    # Quick Examples of copying files in Python
    
    src_path='AIStudyLabs/file.txt'
    dest_path='AIStudyLabs/newfilefile.txt'
    
    # Copies the file's permissions
    import shutil
    shutil.copy(src_path,dest_path)
    
    #  Preserves the file's metadata
    shutil.copy2(src_path,dest_path)
    
    # It doesn't copy the permissions or metadata.
    shutil.copyfile(src_path,dest_path)
    
    # Copy the contents of a file-like object
    shutil.copyfileobj(file, file_like_object, length=16*1024)
    

    <a id="h-2-python-copy-files-using-shutil-module"></a>

    2. Python Copy Files using shutil Module

    The shutil module provides a number of high-level operations on files and collections of files. In particular, it offers a function shutil.copy() and its variants shutil.copy2(), shutil.copyfile() and shutil.copyfileobj() to copy files in Python.

    <a id="h-2-1-copy-files-without-meta-data-using-copyfile"></a>

    2.1 Copy Files without Meta Data using copyfile()

    The copyfile() function is used to copy the contents of one file to another in Python. It does not copy metadata or permissions, and the destination file cannot be a directory. Here, both the source and destination have to be a filename.

    Syntax:

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

    Parameters:

    
    # Copy file
    import shutil
    shutil.copyfile('sourcefile.txt', 'destinationfile.txt')
    
    # Copy with full path
    shutil.copyfile('/src/file/path/sourcefile.txt', '/dest/file/path/destinationfile.txt')
    

    Important Points to Note while Copying Files:

    <a id="h-2-2-copy-files-with-meta-data-using-copy2"></a>

    2.2 Copy Files with Meta Data using copy2()

    This copy2() is used to copy a file from one location to another and preserves the file's metadata, such as timestamp and permissions. The syntax and the parameter list are very much similar to the shutil.copyfile() method.

    Note that it allows dst to be a directory, when used as a directory it copies the file with basename of src.

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

    Below is an example of using shutil.copy2() function.

    
    # Import
    import shutil
    
    # Copies file wih meta data
    shutil.copy2('sourcefile.txt', 'destination.txt')
    
    # Copies the file to another destination directory
    shutil.copy2('/src/path/sourcefile.txt', '/dst/path/destination.txt')
    
    # Copies the same file to the distination directory
    shutil.copy2('/src/path/sourcefile.txt', '/dst/path/')
    

    <a id="h-2-3-copy-only-with-permission-data-using-copy"></a>

    2.3 Copy only with Permission Data using copy()

    The copy() function is used to copy a file from one location to another, it does not copy metadata but preserves file permissions.

    Syntax:

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

    See the following example of using the shutil.copy() function.

    
    # Copy files using copy()
    import shutil
    with open('sourcefile.txt', 'rb') as src_file, open('destfile.txt', 'wb') as dst_file:
        shutil.copyfileobj(src_file, dst_file)
    

    <a id="h-2-4-shutil-copy-vs-shutil-copy2-vs-shutil-copyfile"></a>

    2.4 shutil.copy() vs shutil.copy2() vs shutil.copyfile()

    Below are the comparisons between these three methods. Make sure you choose the right one while working on your project to copy files in Python.

    | Method | Copies Metadata | Copies Permissions | Uses File Object | Destination May Be Directory | | --- | --- | --- | --- | --- | | shutil.copy() | Yes | Yes | No | Yes | | shutil.copyfile() | No | No | No | No | | shutil.copy2() | Yes | Yes | No | Yes |

    In terms of speed, the speed difference between these methods would be negligible for small to medium-sized files.For large files, or when working with many files at once.

    | Method | Speed | | --- | --- | | shutil.copyfile() | Fast | | shutil.copy() | Medium | | shutil.copy2() | Slow |

    <a id="h-3-open-and-write-to-copy-files"></a>

    3. open() and write() to copy files

    The open() function is used to open a file and return a file object, which is a Python object that represents the file. The write() function is used to write data to a file. We can combine both of them to copy files from source to destination in Python.

    
    # Open the source file in read mode
    with open("source_file.txt", "r") as src_file:
        # read the contents of the source file
        src_data = src_file.read()
    
    # Open the destination file in write mode
    with open("destination_file.txt", "w") as dst_file:
        # write the contents of the source file to the destination file
        dst_file.write(src_data)
    

    <a id="h-4-copy-both-folders-and-files-in-python"></a>

    4. Copy both folders and files in python

    Python provides different built-in and third-party modules to copy a single file or an entire folder. The first method is using the built-in shutil.copytree() method, and the second is using shutil.copy2() of shutil.copy() the method in FOR Loop.

    <a id="h-4-1-example-shutil-copytree"></a>

    4.1 Example - shutil.copytree()

    In the below example, source_folder is the folder you want to copy and destination_folder is the new folder to which you want to copy the contents of the source folder.

    
    # Copy directory
    import shutil
    shutil.copytree("source_dir", "destination_dir")
    

    <a id="h-4-2-example-for-loop-with-shutil-copy2"></a>

    4.2 Example - For loop with shutil.copy2()

    If you want to copy only the files in a folder and not the subfolders and its content, you can use shutil.copy() function instead, this will copy all files in the folder to the destination.

    
    # Copy only files not directories
    import os
    import shutil
    def copy_files(src, dst):
        for item in os.listdir(src):
            s = os.path.join(src, item)
            d = os.path.join(dst, item)
            if os.path.isfile(s):
                shutil.copy2(s, d)
    
    copy_files("source_folder", "destination_folder")
    

    5. Copy Files using OS module

    The OS is a standard utility module that provides several functions to interact with the operating system. We can use two functions popen() and system() to copy the files.

    5.1 Using popen()

    Following is the syntax of popen()

    
    # Syntax
    os.popen(cmd[, mode[, bufsize]])
    

    Here is an example using popen() to copy files.

    
    # Example to copy files using popen()
    import os
    
    # In Unix/Linux
    os.popen('cp sourcefile.txt destinationfile.txt') 
    
    # In Windows
    os.popen('copy sourcefile.txt destinationfile.txt')
    

    5.2 Using system()

    Following is the syntax of the os.system()

    
    # Syntax
    os.system(command)
    

    Here is an example using popen() to copy files.

    
    # Example to copy files using popen()
    import os
    
    # In Linux/Unix
    os.system('cp sourcefile.txt destinationfile.txt')  
    
    # In Windows
    os.system('copy sourcefile.txt destinationfile.txt')
    

    6. Copying files using subprocess Module

    The Python subprocess module is used to launch child processes from Python code, It can be used to run the shell commands for UNIX and dos commands from windows.

    6.1 Syntax of subprocess

    Following is the syntax of the subprocess.call()

    
    # Syntax
    subprocess.call(args, *, stdin=None, stdout=None, stderr=None, shell=False)
    

    6.2 Examples

    Following is an example of running a copy command using subprocess.call() to copy a file. based on OS you are running this code, you need to use the right command. For example, cp command is used in UNIX and copy is used in winds to copy files.

    
    # Import
    import subprocess
    
    # Example using subprocess.call()
    # In Linux/Unix
    status = subprocess.call('cp sourcefile.txt destinationfile.txt', shell=True) 
    
    # In Windows
    status = subprocess.call('copy sourcefile.txt destinationfile.txt', shell=True)
    

    <a id="h-7-conclusion"></a>

    7. Conclusion

    We have discussed different methods for copying files in python. By Now you will have a clear understanding of how to move files with metadata and without metadata. Additionally, we compared the different approaches. So make sure you choose the right one that can meet your goals. Let me know if you have any questions.

    References

    <a id="h-related-articles"></a>

    Related Articles