file files - How do I remove/delete a folder that is not empty with Python?
os.remove rename (13)
Base on kkubasik's answer, check if folder exists before remove, more robust
import shutil def remove_folder(path): # check if folder exists if os.path.exists(path): # remove if exists shutil.rmtree(path) remove_folder("/folder_name")
I am getting an 'access is denied' error when I attempt to delete a folder that is not empty. I used the following command in my attempt:
What is the most effective way of removing/deleting a folder/directory that is not empty?
Just some python 3.5 options to complete the answers above. (I would have loved to find them here).
import os import shutil from send2trash import send2trash # (shutil delete permanently)
Delete folder if empty
root = r"C:\Users\Me\Desktop\test" for dir, subdirs, files in os.walk(root): if subdirs ==  and files == : send2trash(dir) print(dir, ": folder removed")
Delete also folder if it contains this file
elif subdirs ==  and len(files) == 1: # if contains no sub folder and only 1 file if files== "desktop.ini" or: send2trash(dir) print(dir, ": folder removed") else: print(dir)
delete folder if it contains only .srt or .txt file(s)
elif subdirs == : #if dir doesn’t contains subdirectory ext = (".srt", ".txt") contains_other_ext=0 for file in files: if not file.endswith(ext): contains_other_ext=True if contains_other_ext== 0: send2trash(dir) print(dir, ": dir deleted")
Delete folder if its size is less than 400kb :
def get_tree_size(path): """Return total size of files in given path and subdirs.""" total = 0 for entry in os.scandir(path): if entry.is_dir(follow_symlinks=False): total += get_tree_size(entry.path) else: total += entry.stat(follow_symlinks=False).st_size return total for dir, subdirs, files in os.walk(root): If get_tree_size(dir) < 400000: # ≈ 400kb send2trash(dir) print(dir, "dir deleted")
def deleteDir(dirPath): deleteFiles =  deleteDirs =  for root, dirs, files in os.walk(dirPath): for f in files: deleteFiles.append(os.path.join(root, f)) for d in dirs: deleteDirs.append(os.path.join(root, d)) for f in deleteFiles: os.remove(f) for d in deleteDirs: os.rmdir(d) os.rmdir(dirPath)
if you are sure, that you want to delete the entire dir tree, and are no more interested in contents of dir, then crawling for entire dir tree is stupidness... just call native OS command from python to do that. It will be faster, efficient and less memory consuming.
RMDIR c:\blah /s /q
rm -rf /home/whatever
In python, the code will look like..
import sys import os mswindows = (sys.platform == "win32") def getstatusoutput(cmd): """Return (status, output) of executing cmd in a shell.""" if not mswindows: return commands.getstatusoutput(cmd) pipe = os.popen(cmd + ' 2>&1', 'r') text = pipe.read() sts = pipe.close() if sts is None: sts = 0 if text[-1:] == '\n': text = text[:-1] return sts, text def deleteDir(path): """deletes the path entirely""" if mswindows: cmd = "RMDIR "+ path +" /s /q" else: cmd = "rm -rf "+path result = getstatusoutput(cmd) if(result!=0): raise RuntimeError(result)
You can use os.system command for simplicity:
import os os.system("rm -rf dirname")
As obvious, it actually invokes system terminal to accomplish this task.
If you don't want to use the
shutil module you can just use the
from os import listdir, rmdir, remove for i in listdir(directoryToRemove): os.remove(os.path.join(directoryToRemove, i)) rmdir(directoryToRemove) # Now the directory is empty of files
import shutil shutil.rmtree(dest, ignore_errors=True)
I have found a very easy way to Delete any folder(Even NOT Empty) or file on WINDOWS OS.
os.system('powershell.exe rmdir -r D:\workspace\Branches\*%s* -Force' %CANDIDATE_BRANCH)
import shutil shutil.rmtree('/folder_name')
rmtree fails on folder trees containing read-only files. If you want the folder to be deleted regardless of whether it contains read-only files, then use
import os import stat import shutil def errorRemoveReadonly(func, path, exc): excvalue = exc if func in (os.rmdir, os.remove) and excvalue.errno == errno.EACCES: # change the file to be readable,writable,executable: 0777 os.chmod(path, stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO) # retry func(path) else: # raiseenter code here shutil.rmtree(path, ignore_errors=False, onerror=errorRemoveReadonly)
If ignore_errors is set, errors are ignored; otherwise, if onerror is set, it is called to handle the error with arguments (func, path, exc_info) where func is os.listdir, os.remove, or os.rmdir; path is the argument to that function that caused it to fail; and exc_info is a tuple returned by sys.exc_info(). If ignore_errors is false and onerror is None, an exception is raised.enter code here
from python 3.4 you may use :
import pathlib def delete_folder(pth) : for sub in pth.iterdir() : if sub.is_dir() : delete_folder(sub) else : sub.unlink() pth.rmdir() # if you just want to delete dir content, remove this line
pth is a
pathlib.Path instance. Nice, but may not be the fastest.
From the python docs on
# Delete everything reachable from the directory named in 'top', # assuming there are no symbolic links. # CAUTION: This is dangerous! For example, if top == '/', it # could delete all your disk files. import os for root, dirs, files in os.walk(top, topdown=False): for name in files: os.remove(os.path.join(root, name)) for name in dirs: os.rmdir(os.path.join(root, name))
You could also use the loadtxt command in NumPy. This checks for fewer conditions than genfromtxt, so it may be faster.
import numpy data = numpy.loadtxt(filename, delimiter="\n")