logo

Python shutil Module Summary 📂Programing

Python shutil Module Summary

Overview 1

shutil is a standard module that gathers high-level commands for files and directories. One of the reasons to use Python is its capability to write programs quickly and conveniently. However, it’s quite inconvenient as the functionalities that logically should be available are scattered between the shutil module and os module. When handling the file system, it’s necessary to use both modules wisely.

Even though this is a comprehensive summary, it’s not just a scraping of the official documentation but explained with a focus on the features used in practice, so not all information is included. If you can’t find the feature you’re looking for in this post, check the official documentation.

The order of the following index is not by the document order but by the order of practical usability based on experience. The more frequently used functions are listed towards the top. Think of the index itself as a cheatsheet for quickly finding the function you want.

Direct methods of shutil

Removal rmtree()

rmtree(path, ignore_errors=False, onerror=None)

  • path: It deletes the entire directory specified. It’s the biggest reason to use the shutil module.

Copy copyfile(), copytree()

copyfile(src, dst, *, follow_symlinks=True)

  • src: The path of the file to be copied. It’s probably an abbreviation of source.
  • dst: The path where the copied file will be saved. It’s probably an abbreviation of destination.

copytree(src, dst, ...)

  • It recursively copies the entire directory tree with src as the root to the dst directory.

Move move()

move(src, dst)

  • It recursively moves the entire directory tree with src as the root to the dst directory.

Environment

  • OS: Windows
  • Python: 3.9.4