[New-bugs-announce] [issue19642] shutil to support equivalent of: rm -f /dir/*

Ivan Radic report at bugs.python.org
Mon Nov 18 12:45:26 CET 2013


New submission from Ivan Radic:

Shutil supports deleting of files and directories but it offers no way to delete directory content (without deleting directory itself). Shell programming includes a lot of "rm -rf /dir" and "rm -f /dir/*", and there is no direct equivalent of these commands in Python. Educate me if I am wrong on this claim.

Sure, I can write a quick for loop, and delete each subfile and subdirectory manually, but adding such ability to shutil would greatly enhance readability and simplicity of Python file handling scripts.

Implementation could be as simply as :
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))

(example taken from http://docs.python.org/2/library/os.html#os.walk)

----------
components: IO
messages: 203283
nosy: ivan.radic
priority: normal
severity: normal
status: open
title: shutil to support equivalent of: rm -f /dir/*
type: enhancement
versions: Python 2.7, Python 3.5

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue19642>
_______________________________________


More information about the New-bugs-announce mailing list