- 1 year ago
Copy folder and contents based on name , without full path
Comment
1 / 1
To copy a folder and its contents based on a specific name, without including the full path, you can use a script in Python. Here's an example using the shutil
module:
pythonimport os
import shutil
source_folder = "/path/to/source/folder"
destination_folder = "/path/to/destination/folder"
folder_name = "desired_folder_name"
# Iterate through the contents of the source folder
for root, dirs, files in os.walk(source_folder):
for dirname in dirs:
if dirname == folder_name:
source_path = os.path.join(root, dirname)
destination_path = os.path.join(destination_folder, dirname)
shutil.copytree(source_path, destination_path)
In this example:
- Set the
source_folder
variable to the path of the folder where you want to search for the desired folder name. - Set the
destination_folder
variable to the path of the folder where you want to copy the folder and its contents. - Set the
folder_name
variable to the name of the folder you want to copy. - The script uses
os.walk
to iterate through the contents of thesource_folder
recursively. - If a directory with the name
folder_name
is found, it usesshutil.copytree
to copy the entire folder and its contents to thedestination_folder
.
Make sure to replace the /path/to/source/folder
and /path/to/destination/folder
with the actual paths on your system.
By running this script, it will search for the folder with the specified name within the source folder and copy it, along with its contents, to the destination folder.