Move all the files from subfolders to the main folder using Python


The given problem statement is to relocate all the files from the subfolders to the main folder using Python. So we will use the OS and Shutil libraries of Python to do this task.

Understanding the Logic for the Problem

In this task, we are required to move all the files that exist in the subfolder in the main folder using Python programming language. So for doing this process we will specify the source folder path and destination or main folder path. And we will be using python’s os and shutil libraries for doing this task. So first we will import the necessary libraries and then create a function to move the files from the subfolder or source folder to the main folder or destination folder.

Algorithm

  • Step 1 − Import the OS and Shutil libraries first.

  • Step 2 − Second, create a function called move_to_main and this function accepts a parameter as the main folder as main_fldr.

  • Step 3 − Then with the help of a for loop we will find the files present in the subfolder.

  • Step 4 − For every file we will change the path from source path to the destination path

  • Step 5 − And then we will use shutil.move to move all the files from source/subfolder to destination/main folder.

Example

import os
import shutil

# Mention the function to move the files
def move_to_main(main_fldr):
   for folder, files in os.walk(main_fldr):
      for file in files:
         source = os.path.join(folder, file)
         destination = os.path.join(main_fldr, file)
         shutil.move(source, destination)
         print(f"Moved {source} to {destination}")


# Mention the path in which the files are located
main_fldr = '/path/to/main/folder'

# Call the main function
move_to_main(main_fldr)

Output

$$\mathrm{Before}$$

$$\mathrm{After}$$

$$\mathrm{Console \: Output}$$

Libraries and Functions Used

In this article we have mainly used two libraries of Python called OS and Shutil.

  • OS Library − The OS library offers functions for adding and deleting the folders or directories and is used to retrieve the contents within the directories. It is also used to change the current directory and much more. So in simple terms we can say that this module offers to utilize operating system features. In this module functions like open, close, write are available.

  • Shutil Library − The Shutil library has many high level processes on files and collections of files. This module mainly provides the tools to facilitate file removal and copying. The shutil module provides functions to use like move, copy, listdir, delete and so on.

  • os.walk() − We have a function in Python’s OS library called walk() which is used to produce the file names in the directory tree by walking the tree in bottom up or top down manner. This function can also be used to search for a specific file in the directory.

  • Shutil.move() − The working mechanism of shutil.move is to relocate the file or folder from one location to another location. And this process is done recursively.

Conclusion

We have successfully moved files from subfolder to the main folder using shutil and os modules present in Python.

Updated on: 16-Oct-2023

263 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements