close

[Solved] Can’t remove a folder with os.remove (WindowsError: [Error 5] Access is denied: ‘c:/temp/New Folder’)

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error Can’t remove a folder with os.remove (WindowsError: [Error 5] Access is denied: ‘c:/temp/New Folder’) in python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How Can’t remove a folder with os.remove (WindowsError: [Error 5] Access is denied: ‘c:/temp/New Folder’) Error Occurs?

Today I get the following error Can’t remove a folder with os.remove (WindowsError: [Error 5] Access is denied: ‘c:/temp/New Folder’) in python.

How To Solve Can’t remove a folder with os.remove (WindowsError: [Error 5] Access is denied: ‘c:/temp/New Folder’) Error ?

  1. How To Solve Can't remove a folder with os.remove (WindowsError: [Error 5] Access is denied: 'c:/temp/New Folder') Error ?

    To Solve Can't remove a folder with os.remove (WindowsError: [Error 5] Access is denied: 'c:/temp/New Folder') Error Remove (delete) the directory path. Only works when the directory is empty, otherwise, OSError is raised.

  2. Can't remove a folder with os.remove (WindowsError: [Error 5] Access is denied: 'c:/temp/New Folder')

    To Solve Can't remove a folder with os.remove (WindowsError: [Error 5] Access is denied: 'c:/temp/New Folder') Error Remove (delete) the directory path. Only works when the directory is empty, otherwise, OSError is raised.

Solution 1

os.remove requires a file path, and raises OSError if path is a directory.

Try os.rmdir(folder+'New Folder')

Which will:

Remove (delete) the directory path. Only works when the directory is empty, otherwise, OSError is raised.

Making paths is also safer using os.path.join:

os.path.join("c:\\", "temp", "new folder")

Solution 2

try the inbuilt shutil module

shutil.rmtree(folder+"New Folder")

this recursively deletes a directory, even if it has contents.

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read