Skip to content
Related Articles

Related Articles

Improve Article
How to move list of folders with subfolders using Python ?
  • Last Updated : 09 May, 2021

Sometimes we need to move an entire directory or maybe there is a list of such directories say A along with its sub-content, files, and subfolders to another destination directory B. While this can be done manually by ‘cutting’ and ‘pasting’ but what if there are hundreds or thousands of directories you want to move let alone the human error! Let’s see how to do this easily in python with few lines of code using shutil module.

The shutil module 

The shutil provides a number of high-level functions that help in automating the process of copying, moving, or removal of files or directories irrespective of the platform used. It comes under Python’s standard utility modules, so there is no need for separate installation. 

It has a shutil.move() method which recursively moves a file or directory (source) along with its sub-content to another location (destination) and returns the destination. If the destination directory already exists then the source is moved inside that directory otherwise a new directory is created before moving. If the destination already exists but is not a directory then it may be overwritten or raise an error depending on os.rename() specifications.

Syntax: shutil.move(source, destination, copy_function = copy2)

Suppose the structure of the directory looks like this –



where ‘dest’ is our destination folder.

Example #1: Based on a list of directories

Here we have made a list of directories we want to move. For the sake of simplicity, all the directories are in the same folder, you can also move from different base directories. 

Python3




# import shutil module
import shutil
  
# import os module
import os
  
# base path
base_path = 'C:/Users/Pulkit/GFG_Articles/root'
  
# list of directories we want to move.
dir_list = ['test2', 'test4', 'test5', 'does_not_exist']
  
# path to destination directory
dest = os.path.join(base_path, 'dest')
  
print("Before moving directories:")
print(os.listdir(base_path))
  
# traverse each directory in dir_list
for dir_ in dir_list:
  
    # create path to the directory in the
    # dir_list.
    source = os.path.join(base_path, dir_)
  
    # check if it is an existing directory
    if os.path.isdir(source):
  
        # move to destination path
        shutil.move(source, dest)
  
print("After moving directories:")
print(os.listdir(base_path))

Output:

Before moving directories:

[‘dest’, ‘test1’, ‘test2’, ‘test3’, ‘test4’, ‘test5’, ‘web_tools_express’, ‘web_tools_html’, ‘web_tools_node’, ‘web_tools_react’]



After moving directories:

[‘dest’, ‘test1’, ‘test3’, ‘web_tools_express’, ‘web_tools_html’, ‘web_tools_node’, ‘web_tools_react’]

Let’s check the destination folder, which is as:

As you can see we moved the entire directories and their contents to the destination directory (absolute path points to ‘dest’ directory).

Example #2: Based on the pattern

Suppose we want to move directories that follow a specific name pattern to our destination. Let’s move all directories whose name starts with ‘web’. You can use any pattern according to your needs. This would be a more practical use case where we have hundreds of directories.

Python3




# import shutil module
import shutil
  
# import os module
import os
  
# base path
base_path = 'C:/Users/Pulkit/GFG_Articles/root'
  
# get all directories in our base path.
all_dir = os.listdir(base_path)
  
# path to destination directory
dest = os.path.join(base_path, 'dest')
  
print("Before moving directories:")
print(os.listdir(base_path))
  
for dir_ in all_dir:
  
    # check if the dir_ follows the required
    # pattern.
    if dir_.startswith('web'):
  
        # create path to this directory.
        source = os.path.join(base_path, dir_)
  
        # move to destination path
        shutil.move(source, dest)
  
print("After moving directories:")
print(os.listdir(base_path))

Output:

Before moving directories:



[‘dest’, ‘test1’, ‘test3’, ‘web_tools_express’, ‘web_tools_html’, ‘web_tools_node’, ‘web_tools_react’]

After moving directories:

[‘dest’, ‘test1’, ‘test3’]

Let’s check the destination folder, which is as:

The destination folder contains all directories from the previous example and this one!

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up
Recommended Articles
Page :