Delete Root Directory in Python

Asked

Viewed 327 times

1

I am creating a system that erases and creates again the folders of a directory until the creation of the folders is all ok, but I tried to use the calling below to delete a Root directory of my application but it only accepts to delete empty folders. Can anyone tell me anything I can do to delete the complete directory.

follows exexmplo below

import os

#variaveis
user00= os.listdir(r'D:\Publico\PA00_SEDE')
user99 = os.listdir(r'D:\Publico\PA99_ADMINISTRATIVO')
users = os.listdir(r'D:\Publico\PA01_FARMÁCIA')
users7 = os.listdir(r'D:\Publico\PA02_7_SETEMBRO')
users15 = os.listdir(r'D:\Publico\PA03_15_NOVEMBRO')
users4 = os.listdir(r'D:\Publico\PA04_JUNDIAÍ')


#Neste ponto é onde tenho o problema
#Este é o diretório raiz de outras varias subpastas, porem nao posso apagar com
#o metodo abaixo pois ele só apaga diretórios vazios
***os.rmdir(r'D:\Publico')*** 


#laços para criar as pastas
for x in user00:
    os.makedirs(r'D:\Publico\PA00_SEDE\\'+x)

for x in user99:
    os.makedirs(r'D:\Publico\PA99_ADMINISTRATIVO\\'+x)


for x in users:
    os.makedirs(r'D:\Publico\PA01_FARMÁCIA\\'+x)

for x in users7:
    os.makedirs(r'D:\Publico\PA02_7_SETEMBRO\\'+x)

for x in users15:
    os.makedirs(r'D:\Publico\PA03_15_NOVEMBRO\\'+x)

for x in users15:
    os.makedirs(r'D:\Publico\PA04_JUNDIAÍ\\'+x)

2 answers

3

The function os.rmdir directly calls the OS Api equivalent to this call, and traditionally this call only erases even an empty directory.

However, the Python language includes, in addition to these direct calls, the module shutil that has some facilities for handling files. Among them is the shutil.rmtree - this yes, a call that receiving a directory as argument, erases all contents beyond the directory itself:

import shutil
...
shutil.rmtree(r'D:\Publico')
  • great was exactly what I needed the 2 answers will serve directly in the API I’m developing , thanks!

0

In order to be able to delete the directory, you must first delete the files and folders inside it. Use the function walk to go through all files and subfolders of the root directory and delete them using the remove.

Example:

import os

def rmdir(path):
    for dir_,dirnames,filenames in list(os.walk(path))[::-1]:
        for filename in filenames:
            filename = os.path.normpath(dir_+'\\'+filename)
            print("Removendo",filename,"...")
            os.remove(filename)
        print("Removendo",os.path.normpath(dir_),"...")
        os.rmdir(dir_)

rmdir("<Coloque aqui seu diretório>")

In the above example I am going through all directories and files and removing them one by one. The function walk returns a generator, and for everything to work, I need to delete the most internal files and folders first. For that, I turn it into a list and invert its order.

In addition you can also use the function rmtree module shutil. This function will remove the directory without first deleting the files inside.

  • Can use the shutil.rmtree to remove a non-empty directory.

  • great was exactly what I needed the 2 answers will serve directly in the API I’m developing , thanks!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.