This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author ivan.radic
Recipients ivan.radic
Date 2013-11-18.11:45:25
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1384775126.27.0.181995142425.issue19642@psf.upfronthosting.co.za>
In-reply-to
Content
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)
History
Date User Action Args
2013-11-18 11:45:26ivan.radicsetrecipients: + ivan.radic
2013-11-18 11:45:26ivan.radicsetmessageid: <1384775126.27.0.181995142425.issue19642@psf.upfronthosting.co.za>
2013-11-18 11:45:26ivan.radiclinkissue19642 messages
2013-11-18 11:45:25ivan.radiccreate