Copying folder with Python

12 Feb, 2022

1 Mins Read

Python is powerful like anyother programming language. However the simplicity of it makes it more beautiful.

I was trying to automate some of my tasks and this needed copying of a folder with all its files and folders and files inside those folders to a new location. With Python it just took some lines and the task is done!

import shutil
shutil.copytree(source_folder, destination_folder, dirs_exist_ok = True)

The shutil is a utility to deal with file operations. The trick here is dirs_exist_ok parameter. The official document says it is a new addition in version 3.8.

New in version 3.8: The dirs_exist_ok parameter.

This parameter makes it easy to copy files inside an already existing folder with files inside of it.