How to move list of folders with subfolders using Python?


Moving a list of folders with subfolders is a common task when working with large files or organizing data. Python offers several approaches to handle this task, each with their own advantages and disadvantages.

In this article, we will learn how to move a list of folders with subfolders using Python. We will see the different approaches along with their syntaxes and examples to moving a folder list with subfolders using Python.

How python helps in moving list of folders with subfolders?

Python is a popular programming language that provides a variety of built−in functions and modules to handle different types of data, including lists and folders. In the context of moving a list of folders with subfolders, Python's built−in os and shutil modules can be helpful.

The os module provides a platform−independent way of working with operating system functionalities, including file and directory operations. The shutil module, on the other hand, provides higher−level file operations that are easier to use than the os module.

To move a list of folders with subfolders, you can use the os module's listdir() function to get a list of all the files and directories in the source directory. Then, you can use a for loop to iterate over each item in the list and use the shutil module's move() function to move the folders to the destination directory.

It's worth noting that when moving a folder with subfolders, you'll also need to move all the subfolders and their contents recursively. This can be achieved using a recursive function that calls itself for each subdirectory.

Approaches

There are different approaches to moving a list of folders with subfolders using python, let’s see some of the common approaches used to move list of folders with subfolders:

Approach 1: Using shutil.move()

The shutil module provides a high−level interface for handling file operations. The move() method of shutil can be used to move files or folders from one location to another. The method accepts two arguments: source and destination. The source argument is the path of the file or folder that needs to be moved, while the destination argument is the path where the file or folder needs to be moved.

Syntax

The below syntax defines the usage of shutil.move()

import shutil
shutil.move(src, dst)

Example

In the given example, we first import the shutil and os modules. We then define the source_folder and destination_folder variables as the paths to the source and destination folders respectively.

Next, we use os.listdir() to get a list of all files and directories in the source_folder. We then use a list comprehension to filter out only the directories using os.path.isdir(). We then use os.path.join() to create the full path to each directory.

Finally, we loop over the subdirs list and use shutil.move() to move each subdirectory to the destination_folder.

#import the required modules
import shutil
import os

#define the source folder and destination folder paths
my_source_folder = 'C:/Users/username/Documents/my_source_folder'
my_destination_folder = 'C:/Users/username/Documents/my_destination_folder'

# Get a list of all subdirectories in the source folder
my_subdirs = [os.path.join(my_source_folder, o) for o in os.listdir(my_source_folder) if os.path.isdir(os.path.join(my_source_folder, o))]

# Moving each subdirectory to the destination folder
for subdir in my_subdirs:
    shutil.move(subdir, my_destination_folder)

Output

Before:

After:

Approach 2: Using os.rename()

The os module provides a lower−level interface for file operations. The rename() method of os can be used to rename files or folders, but it can also be used to move files or folders by renaming them to a new path.

Syntax

The below syntax defines the usage of os.rename()

import os
os.rename(src, dst)

Example

In the given example, we first import the os module. We then define the source_folder and destination_folder variables as the paths to the source and destination folders respectively.

Next, we use os.listdir() to get a list of all files and directories in the source_folder. We then use a list comprehension to filter out only the directories using os.path.isdir(). We then use os.path.join() to create the full path to each directory.

Finally, we loop over the subdirs list and use os.rename() to move each subdirectory to the destination_folder. We create the new path to the subdirectory in the destination_folder using os.path.join() and os.path.basename().

#import the required modules
import shutil
import os

#define the source folder and destination folder paths
my_source_folder = 'C:/Users/username/Documents/my_source_folder'
my_destination_folder = 'C:/Users/username/Documents/my_destination_folder'

# Get a list of all subdirectories in the source folder
my_subdirs = [os.path.join(my_source_folder, o) for o in os.listdir(my_source_folder) if os.path.isdir(os.path.join(my_source_folder, o))]

# Moving each subdirectory to the destination folder
for subdir in my_subdirs:
    new_path = os.path.join(my_destination_folder, os.path.basename(subdir))
    os.rename(subdir, new_path)

Output

Before:

After:

Approach 3: Using shutil.copytree() and shutil.rmtree()

The shutil module provides two more methods: copytree() and rmtree(). copytree() can be used to copy an entire directory tree from one location to another, while rmtree() can be used to delete an entire directory tree.

Syntax

The below syntax defines the usage of shutil.copytree() and shutil.rmtree()

import shutil
shutil.copytree(src, dst)
shutil.rmtree(path)

Example

In the given example, we first import the shutil and os modules. We then define the source_folder and destination_folder variables as the paths to the source and destination folders respectively.

Next, we use os.listdir() to get a list of all files and directories in the source_folder. We then use a list comprehension to filter out only the directories using os.path.isdir(). We then use os.path.join() to create the full path to each directory.

Finally, we loop over the subdirs list and use shutil.copytree() to copy each subdirectory to the destination_folder. We create the new path to the subdirectory in the destination_folder

#import the required modules
import shutil
import os

#define the source folder and destination folder paths
my_source_folder = 'C:/Users/username/Documents/my_source_folder'
my_destination_folder = 'C:/Users/username/Documents/my_destination_folder'

# Get a list of all subdirectories in the source folder
my_subdirs = [os.path.join(my_source_folder, o) for o in os.listdir(my_source_folder) if os.path.isdir(os.path.join(my_source_folder, o))]

# Copy each subdirectory to the destination folder
for subdir in my_subdirs:
    my_new_path = os.path.join(my_destination_folder, os.path.basename(subdir))
    shutil.copytree(subdir, my_new_path)
    
    # Removing the original subdirectory
    shutil.rmtree(subdir)

Output

Before:

After:

Conclusion

Moving a list of folders with subfolders is an essential task when working with large files or organizing data, and Python offers several approaches to handle it. In this article, we have discussed the three most common approaches: using shutil.move(), os.rename(), and shutil.copytree() and shutil.rmtree(). Each of these approaches has its advantages and disadvantages, and the best approach depends on the specific use case. Therefore, it is essential to choose the approach that best fits the requirements of the task at hand.

Updated on: 31-Aug-2023

241 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements