How do I delete a file or folder in Python?
Here is a robust function that uses both os.remove and shutil.rmtree:
def remove(path):
""" param could either be relative or absolute. """
if os.path.isfile(path) or os.path.islink(path):
os.remove(path) # remove the file
elif os.path.isdir(path):
shutil.rmtree(path) # remove dir and all contains
else:
raise ValueError("file {} is not a file or dir.".format(path))