shutil
Resources
- Official documentation of the shutil module.
Copying, moving and deleting files and directories
If you need to copy, move or delete a file or directory you can use the shutil utilities.
- shutil.copy(path_to_file1, path_to_file2) copies the file in path_to_file1 src to the file path_to_file2.
- shutil.copytree(path_to_dir1, path_to_dir2) copies recursively the contents of path_to_dir1 to path_to_dir2
- shutil.rmtree(path_to_dir) deletes the entire directory tree.
- shutil.move(path1, path2) moves the file or directory from path1 to path2.
To remove a file, not a directory, you could use the unlink method of a Path object.
Exercises
Warning: Be very careful with rmtree because it could delete your files.
Create a directory named project1, with the files analysis_result1.txt and analysis_result2.txt in it.
Tip
Now remove the file analysis_result2.txt.
Tip
Now remove the project dir recursively, including all its contents. Be very careful with the use of rmtree
Tip