Deleting folders in python recursively

前端 未结 10 2098
梦毁少年i
梦毁少年i 2020-12-04 08:51

I\'m having a problem with deleting empty directories. Here is my code:

for dirpath, dirnames, filenames in os.walk(dir_to_search):
    //other codes

    try         


        
10条回答
  •  失恋的感觉
    2020-12-04 09:30

    Just for the next guy searching for a micropython solution, this works purely based on os (listdir, remove, rmdir). It is neither complete (especially in errorhandling) nor fancy, it will however work in most circumstances.

    def deltree(target):
        print("deltree", target)
        for d in os.listdir(target):
            try:
                deltree(target + '/' + d)
            except OSError:
                os.remove(target + '/' + d)
    
        os.rmdir(target)
    

提交回复
热议问题