मैं पाइथन में संभावित रूप से गैर-खाली डीआईआर कैसे हटा सकता हूं।पायथन: गैर-खाली डीआईआर हटाएं
निर्देशिका में नेस्टेड उपनिर्देशिका कई स्तरों को गहरा कर सकती है।
मैं पाइथन में संभावित रूप से गैर-खाली डीआईआर कैसे हटा सकता हूं।पायथन: गैर-खाली डीआईआर हटाएं
निर्देशिका में नेस्टेड उपनिर्देशिका कई स्तरों को गहरा कर सकती है।
उपयोग shutil.rmtree
:
import shutil
shutil.rmtree(path)
कैसे संभाल और/या त्रुटियों को अनदेखा करने के लिए के विवरण के लिए the documentation देखें।
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.
मानक पुस्तकालय भी शामिल है 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)>
पास करके और अधिक परिष्कृत त्रुटि हैंडलिंग प्रदर्शन कर सकते हैं।
यदि निर्देशिका में फ़ाइलें हैं तो यह मेरे लिए विफल हो जाती है। डघबल की पोस्ट देखें। – CornSmith