Skip to content

Instantly share code, notes, and snippets.

@ArthurDelannoyazerty
Created May 17, 2024 08:21
Show Gist options
  • Save ArthurDelannoyazerty/0c4b1a7b0473e8d4ec8a90ed08378612 to your computer and use it in GitHub Desktop.
Save ArthurDelannoyazerty/0c4b1a7b0473e8d4ec8a90ed08378612 to your computer and use it in GitHub Desktop.
Recursively delete the content of a folder
def delete_content_folder(folder:str):
for filename in tqdm(os.listdir(folder), desc="Erasing Temporary Data", leave=False):
file_path = os.path.join(folder, filename)
try:
if os.path.isfile(file_path) or os.path.islink(file_path):
os.unlink(file_path)
elif os.path.isdir(file_path):
shutil.rmtree(file_path)
except Exception as e:
print('Failed to delete %s. Reason: %s' % (file_path, e))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment