Python is a versatile programing language , and one of its strengths lies in its extensive standard library. One important module in this library is shutil
, which provides powerful file and directory manipulation utilities. In this blog post, we’ll Learn how to use Python shutil module to copy, move, and delete files and directories with simple, practical examples.
Introduction to shutil
The 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 shutil
.
1. shutil.copyfile(src, dst)
- The
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)
- The
shutil.copy(src, dst)
function is similar toshutil.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 toshutil.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')
4. shutil.rmtree(path)
- The
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 (dst
).- 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 )
Conclusion
The shutil
module in Python provides a powerful set of tools for simplifying file operations. Whether you need to copy, move, or delete files and directories, shutil
offers easy-to-use functions to streamline these tasks. By incorporating shutil
into your Python projects, you can enhance your file management capabilities and make your code more efficient and robust.
In this blog post, we’ve covered some of the most commonly used functions in shutil
. However, there are many more functions and options available in the module to suit your specific needs. To explore further, refer to the official Python documentation or consult additional resources to unlock the full potential of shutil
in your Python programming journey. You can also explore more about file handling in python at the official documentation here
Happy Learning!!