The shutil
module in Python is a built-in module that provides a higher-level interface for file operations. It contains functions for copying, moving, renaming, and deleting files and directories.
Here are some commonly used functions in the shutil
module:
shutil.copy(src, dst)
: Copies the file at the pathsrc
to the pathdst
. Ifdst
is a directory, the file is copied with its original name into the directory. Ifdst
is a file, the file atsrc
is copied and renamed todst
.shutil.copy2(src, dst)
: Copies the file at the pathsrc
to the pathdst
, preserving the file metadata (such as creation and modification times).shutil.copytree(src, dst)
: Recursively copies the directory at the pathsrc
to the pathdst
.shutil.move(src, dst)
: Moves the file or directory at the pathsrc
to the pathdst
. Ifdst
is a directory, the file or directory atsrc
is moved with its original name into the directory. Ifdst
is a file, the file or directory atsrc
is moved and renamed todst
.shutil.rmtree(path)
: Recursively deletes the directory at the pathpath
.
These are just a few examples of the functions available in the shutil
module. It’s a useful module for manipulating files and directories in Python.
Working of Shutil Module:
The shutil
module in Python provides a higher-level interface for file and directory operations, making it easier to manipulate files and directories in Python.
Here is a brief overview of how some of the functions in the shutil
module work:
shutil.copy(src, dst)
: This function copies the file at the pathsrc
to the pathdst
. Ifdst
is a directory, the file is copied with its original name into the directory. Ifdst
is a file, the file atsrc
is copied and renamed todst
. This function raises an error ifdst
already exists.shutil.copy2(src, dst)
: This function works similarly toshutil.copy()
, but it also preserves the file metadata (such as creation and modification times).shutil.copytree(src, dst)
: This function recursively copies the directory at the pathsrc
to the pathdst
. This function raises an error ifdst
already exists.shutil.move(src, dst)
: This function moves the file or directory at the pathsrc
to the pathdst
. Ifdst
is a directory, the file or directory atsrc
is moved with its original name into the directory. Ifdst
is a file, the file or directory atsrc
is moved and renamed todst
. This function raises an error ifdst
already exists.shutil.rmtree(path)
: This function recursively deletes the directory at the pathpath
. This function raises an error if the directory atpath
does not exist or is not empty.
These functions use the underlying file system APIs to perform the operations, which means that their behavior is dependent on the operating system and file system being used. It’s important to use these functions carefully and make sure to handle errors appropriately, especially when deleting files and directories.
Copy Files:
To copy a file in Python using the shutil
module, you can use the shutil.copy(src, dst)
function. The src
argument is the path to the source file that you want to copy, and the dst
argument is the path to the destination file.
Here’s an example of how to copy a file named file.txt
from the current directory to a directory named backup
:
import shutil src_file = "file.txt" dst_dir = "backup/" shutil.copy(src_file, dst_dir)
In this example, src_file
is the path to the source file (file.txt
) and dst_dir
is the path to the destination directory (backup/
). The copy
function copies the file to the backup/
directory with the same name (file.txt
).
Note that if the destination file already exists, the copy
function will raise a FileExistsError
. If you want to overwrite the destination file, you can use the shutil.copy2(src, dst)
function instead, which will preserve the original file’s metadata (such as creation and modification times) in the copy.
The copy2() Function:
The shutil.copy2(src, dst)
function is similar to shutil.copy(src, dst)
in that it copies a file from the source path src
to the destination path dst
. However, shutil.copy2()
also preserves the metadata of the original file, including the file’s permissions, timestamps, and other attributes.
Here’s an example of how to use shutil.copy2()
to copy a file:
import shutil src_file = "file.txt" dst_dir = "backup/" shutil.copy2(src_file, dst_dir) import shutil src_file = "file.txt" dst_dir = "backup/" shutil.copy2(src_file, dst_dir)
In this example, src_file
is the path to the source file (file.txt
) and dst_dir
is the path to the destination directory (backup/
). The copy2
function copies the file to the backup/
directory with the same name (file.txt
) while preserving its original metadata.
Note that if the destination file already exists, shutil.copy2()
will overwrite it without raising an error. Also, if the source file doesn’t exist, shutil.copy2()
will raise a FileNotFoundError
.
The shutil.copyfile() Function:
The shutil.copyfile(src, dst)
function is another option for copying a file in Python using the shutil
module. This function copies the contents of the source file src
to the destination file dst
. Unlike shutil.copy()
and shutil.copy2()
, shutil.copyfile()
doesn’t preserve the metadata of the original file.
Here’s an example of how to use shutil.copyfile()
to copy a file:
import shutil src_file = "file.txt" dst_file = "backup/file.txt" shutil.copyfile(src_file, dst_file)
In this example, src_file
is the path to the source file (file.txt
) and dst_file
is the path to the destination file (backup/file.txt
). The copyfile()
function copies the contents of the source file to the destination file.
Note that if the destination file already exists, shutil.copyfile()
will overwrite it without raising an error. If you want to preserve the metadata of the original file while copying, you can use shutil.copy2()
instead.
The shutil.copytree() Function:
The shutil.copytree(src, dst)
function in Python is used to recursively copy an entire directory from the source path src
to the destination path dst
. The src
argument is the path to the source directory, and the dst
argument is the path to the destination directory.
Here’s an example of how to use shutil.copytree()
to copy a directory:
import shutil src_dir = "my_project/" dst_dir = "backup/" shutil.copytree(src_dir, dst_dir)
In this example, src_dir
is the path to the source directory (my_project/
) and dst_dir
is the path to the destination directory (backup/
). The copytree()
function copies the entire contents of the source directory to the destination directory recursively.
Note that if the destination directory already exists, shutil.copytree()
will raise a FileExistsError
. If you want to overwrite the destination directory, you can use the shutil.rmtree()
function to remove the directory first, or you can use the shutil.copy2()
function to copy the contents of the directory while preserving their metadata.
The shutil.rmtree():
The shutil.rmtree(path)
function in Python is used to recursively delete an entire directory tree. The path
argument is the path to the directory to be deleted.
Here’s an example of how to use shutil.rmtree()
to delete a directory:
import shutil dir_to_delete = "my_project/" shutil.rmtree(dir_to_delete)
In this example, dir_to_delete
is the path to the directory to be deleted (my_project/
). The rmtree()
function deletes the entire directory tree, including all subdirectories and files.
Note that shutil.rmtree()
is a very powerful function that can permanently delete data. So you should be careful while using this function and ensure that you have backed up any important data before deleting any directories.
The shutil.which() Function:
The shutil.which(cmd, mode=os.F_OK | os.X_OK, path=None)
function in Python is used to search for an executable file named cmd
in the directories listed in the path
argument. If the file is found and is executable, the function returns the path to the file. If the file is not found or is not executable, the function returns None
.
Here’s an example of how to use shutil.which()
to find the path to an executable:
import shutil path_to_executable = shutil.which("python") if path_to_executable is not None: print(f"Found Python at {path_to_executable}") else: print("Python not found")
In this example, shutil.which()
is used to search for the python
executable in the directories listed in the PATH
environment variable. If python
is found and is executable, the path to the python
executable is printed. Otherwise, the message “Python not found” is printed.
Note that shutil.which()
can be used to search for any executable file, not just Python executables. The mode
argument can be used to specify the permissions required for the file (default is os.F_OK | os.X_OK
, which means the file must exist and be executable). If path
is not specified, the directories listed in the PATH
environment variable are searched.