Remove a folder that is not empty

EasyOS Module Programs · 9 of 49

Write a Python program to remove a directory that contains files.

Advertisement
Output
False

Approach

  1. shutil.rmtree() deletes a folder and everything inside it.

Solution

Python
import osimport shutil os.makedirs("full_dir/sub", exist_ok=True)open("full_dir/sub/a.txt", "w").close()shutil.rmtree("full_dir")print(os.path.exists("full_dir"))
Output
False
Watch out

rmtree deletes permanently. Double-check the path before calling it.

Learn the concept

New to this topic? Read the lesson, then come back.

More os module programs

← All 49 OS Module Programs

Leave a Comment