How do I remove/delete a folder that is not empty with Python?
How do I remove/delete a folder that is not empty with Python?
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: os.remove("/folder_name")
.
What is the most effective way of removing/deleting a folder/directory that is not empty?
Answer by ddaa for How do I remove/delete a folder that is not empty with Python?
import shutil shutil.rmtree('/folder_name')
Standard Library Reference: shutil.rmtree.
Answer by kkubasik for How do I remove/delete a folder that is not empty with Python?
From the python docs on os.walk()
:
# 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))
Answer by P M for How do I remove/delete a folder that is not empty with Python?
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
or *nix
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]!=0): raise RuntimeError(result[1])
Answer by Siva Mandadi for How do I remove/delete a folder that is not empty with Python?
import shutil shutil.rmtree(dest, ignore_errors=True)
Answer by shiny for How do I remove/delete a folder that is not empty with Python?
shutil.rmtree(path,ignore_errors=False,onerror=errorRemoveReadonly) def errorRemoveReadonly(func, path, exc): excvalue = exc[1] 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
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
Answer by yota for How do I remove/delete a folder that is not empty with Python?
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()
where pth
is a pathlib.Path
instance. Nice, but slow.
Answer by Charles Chow for How do I remove/delete a folder that is not empty with Python?
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")
Answer by lavee singh for How do I remove/delete a folder that is not empty with Python?
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.
Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72
0 comments:
Post a Comment