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.

How to move list of folders with subfolders using Python ?

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.

Similar Reads

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...

How to move list of folders with subfolders using Python ?

Example 1: Based on a list of directories...