Python is a versatile programming language, and one of its strengths lies in its extensive standard library, which includes a module called
shutil. This module, short for “shell utilities,” provides a wide range of file operations that make working with files and directories in Python a breeze. In this blog post, we’ll explore how to copy files and directories using
shutil, along with some of its most commonly used functions.
Introduction to shutil
shutil module is a part of Python’s standard library, which means you don’t need to install any additional packages to use it. It offers high-level file operations, making it easier to perform tasks like copying, moving, and deleting files and directories. Let’s dive into some of the most frequently used functions provided by
1. shutil.copyfile(src, dst)
shutil.copyfile(src, dst)function allows you to copy the contents of one file (
src) to another file (
dst) in the most efficient way possible .T
- This function is useful when you want to create an exact duplicate of a file.
import shutil shutil.copyfile('source_file.txt','destination_file.txt')
2. shutil.copy(src, dst)
shutil.copy(src, dst)function is similar to
shutil.copyfile(), but it copies both the file’s contents and its metadata (such as permissions).
- This function is handy when you want to preserve the original file’s attributes.
- If the destination location already exists, the original file will be overwritten.
import shutil shutil.copy('source_file.txt', 'destination_file.txt')
3. shutil.copy2(src, dst)
shutil.copy2(src, dst)is almost identical to
shutil.copy(), with one key difference: it also attempts to preserve the original timestamps associated with the file.
- This can be useful for maintaining information about when the file was created or modified.
import shutil shutil.copy2('source_file.txt','destination_file.txt')
shutil.rmtree(path)function is used to remove an entire directory and its contents recursively.
- Be cautious when using this function, as it will irreversibly delete everything within the specified directory.
- This function is similar to using the rm -rf command in shell.
import shutil directory_to_delete = 'my_directory' shutil.rmtree(directory_to_delete)
5. shutil.move(src, dst)
- If you need to move a file or directory to a different location,
shutil.move(src,dst)is the function to use.
- It is versatile and can be used for both renaming and moving files and directories.
import shutil source = 'C:/Users/source_file.txt' destination = 'C:/Users/source_file.txt' shutil.move(source, destination)
6. shutil.copytree(src, dst)
shutil.copytree(src, dst)is used to recursively copy an entire directory tree from the source (
src) to the destination (
- This function is helpful for duplicating entire directories, along with their contents and subdirectories.
- If the destination location already exist,the original directory will be merged with it.
import shutil source = 'source_directory'
destination = 'destination_directory' shutil.copytree(source,destination )