Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing sub-folders in Python

Tags:

python

shutil

I have an main folder(map) under this main have sub folder (zoom1,zoom2,zoom3...) how can i remove sub folder using shutil. note * : I know the main folder path sub folders are dynamically created

like image 923
arun kumar Avatar asked Oct 31 '22 18:10

arun kumar


1 Answers

If you're using linux you could do the following. Use python's glob library

Lets you have 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-style wildcard, glob supports shell-style wildcards.

sub_folders_list are a list of folders and then we use shutil.rmtree to remove it.

like image 126
tockards Avatar answered Nov 10 '22 22:11

tockards