Copy a File using Python

The shutil module is a built-in Python module that provides a higher-level interface for performing various file and directory operations. It is mainly used for file operations such as copying, moving, and deleting files and directories.

Python copy file

To copy a file in Python, you need to import the shutil module. The shutil module provides several functions for working with files and directories. Following are the steps to copy file in Python.
  1. Import the shutil module
  2. Specify the source file and destination file paths
  3. Use the shutil.copy() function to copy the file

Import the shutil module

import shutil

Specify the source file and destination file paths

Next, you need to specify the paths of the source file and the destination file. You can do this by assigning the paths to variables.
source_file = 'path/to/source/file' destination_file = 'path/to/destination/file'

Make sure to replace "path/to/source/file" and "path/to/destination/file" with the actual file paths on your system.

Use the shutil.copy() function to copy the file

Finally, you can use the shutil.copy() function to copy the file from the source path to the destination path.
shutil.copy(source_file, destination_file)
The shutil.copy() function takes two arguments: the source file path and the destination file path.

Following is the complete Python code:

import shutil source_file = 'path/to/source/file' destination_file = 'path/to/destination/file' shutil.copy(source_file, destination_file)

Your file should now be copied from the source path to the destination path.

Python shutil module

The Python shutil module contains functions that are used to automate common file operations in a platform-independent way. These functions make it easy to perform file operations, without worrying about platform-specific differences or low-level file system details.
How to Copy a File With Python

Following are some of the most common tasks that can be performed with the shutil module:

  1. Copying files and directories
  2. Moving files and directories
  3. Deleting files and directories
  4. Creating and extracting archive files
  5. Changing file permissions
  6. Finding files with specific patterns
The shutil module is included in Python's standard library, which means that it is available on all major operating systems (Windows, Linux, and macOS) without requiring any external dependencies.

Following are some important methods in the Python shutil module, along with examples:

Moving files and directories

Syntax:
shutil.move(src, dst)
The move() method is used to move a file or directory from one location to another. The src parameter specifies the source file or directory and the dst parameter specifies the destination location where the file or directory is to be moved.
import shutil src_location = "C:/Users/User/Desktop/Source" dst_location = "C:/Users/User/Desktop/Destination" # Move the entire Source folder to Destination folder shutil.move(src_location, dst_location)

In the above example, the Source folder located on the Desktop is moved to the Destination folder on the Desktop.

Deleting files and directories

Syntax:
shutil.rmtree(path)
The rmtree() method is used to delete an entire directory tree including all the files and subdirectories it contains.
import shutil directory_path = "C:/Users/User/Desktop/TestFolder" # Delete the TestFolder and all its contents shutil.rmtree(directory_path)

In the above example, the TestFolder and all its contents are deleted from the Desktop.

Creating and extracting archive files

Syntax:
shutil.make_archive(base_name, format, root_dir)
The make_archive() method is used to create an archive file (like a zip file) containing the files from the specified directory.
import shutil directory_path = "C:/Users/User/Desktop/TestFolder" archive_file = "C:/Users/User/Desktop/TestArchive" # Create a zip archive file of the TestFolder shutil.make_archive(archive_file, 'zip', directory_path)

In the above example, an archive file named TestArchive.zip is created on the Desktop containing the files from the TestFolder directory.

These are just a few examples of the many useful methods available in the Python shutil module.