2009-10-12 13 views

उत्तर

44

उपयोग shutil.rmtree:

import shutil 

shutil.rmtree(path) 

कैसे संभाल और/या त्रुटियों को अनदेखा करने के लिए के विवरण के लिए the documentation देखें।

+13

यदि निर्देशिका में फ़ाइलें हैं तो यह मेरे लिए विफल हो जाती है। डघबल की पोस्ट देखें। – CornSmith

7

आप shutil.rmtree

shutil.rmtree(path[, ignore_errors[, onerror]])

Delete an entire directory tree; path must point to a directory (but not a symbolic link to a directory). If ignore_errors is true, errors resulting from failed removals will be ignored; if false or omitted, such errors are handled by calling a handler specified by onerror or, if that is omitted, they raise an exception.

32
इस के लिए

मानक पुस्तकालय भी शामिल है shutil.rmtree चाहते हैं। डिफ़ॉल्ट रूप से,

shutil.rmtree(path) # errors if dir not empty 

OSError: [Errno 66] Directory not empty: <your/path> देगा।

आपको त्रुटि की अनदेखी करके वैसे भी निर्देशिका और उसकी सामग्री को हटा सकते हैं:

shutil.rmtree(role_fs_path, ignore_errors=True) 

तुम भी onerrror=<some function(function, path, excinfo)> पास करके और अधिक परिष्कृत त्रुटि हैंडलिंग प्रदर्शन कर सकते हैं।

+4

'ignore_errors = True' का अर्थ है कि यह निर्देशिका को नहीं हटाता है। – ostrokach

+0

ignore_errors = सच था tickket –

+0

यह मेरे लिए काम करता है। – Jerome

संबंधित मुद्दे