Removing subfolders in Python

I have a main folder (map) under this main folder with a subfolder (zoom1, zoom2, zoom3 ...) how can I delete a subfolder using shutil. note *: I know that the main folders in the folder are dynamically created

+5
source share
1 answer

If you are using linux, you can do the following. Use python glob library

Allows you to create a directory structure with the following structure.

  • /map

    • / map / Zoom1 /

    • / map / Zoom2 /

    • / map / Zoom3 /

Using glob and shutil

import glob import shutil sub_folders_pathname = '/map/zoom*/' sub_folders_list = glob.glob(sub_folder_pathname) for sub_folder in sub_folders_list: shutil.rmtree(sub_folder) 

sub_folders_pathname is a shell wildcard, glob supports shell wildcards.

sub_folders_list is a list of folders, and we use shutil.rmtree to delete it.

+1
source

Source: https://habr.com/ru/post/1232934/


All Articles