Z77
Z77

Reputation: 1165

Delete files and subfolders with files in folder

i am using following code to delete all files in one folder that consist of zip file I will not to be deleted and one txt file and one subfolder that consist of lot of txt files and two subfolders with files:

for file_object in os.listdir(folder_path):
    file_object_path = os.path.join(folder_path, file_object)
    print file_object_path
    if os.path.isfile(file_object_path):
        if 'zip.zip' in file_object:
            pass
        else:
            print ('       Deleting file ' + file_object)
            os.unlink(file_object_path)
    else:
        print ('       Deleting folder ' + file_object)
        rmtree(file_object_path)

But result is Error 5 Access denied to delete subfolder. Other files in main folder has not been deleted because first file to be checked was actually subfolder with access denied. I hope someone understand description of problem I have :)

Upvotes: 1

Views: 1866

Answers (1)

Babu
Babu

Reputation: 2598

python-shutil-rmtree-fails. I missed to notice you already use rmtree.

Upvotes: 3

Related Questions