← Back to Python Course | Chapter 9: File Handling | Lesson 6 of 6

Python Files हटाना

Files पढ़ना और लिखना ज़्यादातर रोज़मर्रा की file handling को कवर करता है, पर अंततः किसी file (या पूरे folder) को पूरी तरह हटाना होता है -- Python का os module एक single file के लिए os.remove() और खाली folder के लिए os.rmdir() देता है, जबकि shutil.rmtree() किसी folder और उसके अंदर मौजूद सब कुछ संभालता है।
Syntax
python
import os

if os.path.exists("file_name"):
    os.remove("file_name")

os.remove() से File हटाना

os.remove(path) दिए गए path पर मौजूद file को permanently delete कर देता है -- इसमें कोई built-in undo नहीं है, इसलिए इस तरह हटाई गई file वाकई गायब हो जाती है (सिवाय Python के बाहर मौजूद किसी operating-system-level recovery tool के)।

Note: os.remove() चलाने से पहले उसे दिए जा रहे exact path को दोबारा जाँच लें, खासकर उन scripts में जो paths को dynamically बनाती हैं -- एक गलत path बिना किसी चेतावनी के गलत file हटा देता है।
Warning: os.remove() कोई confirmation prompt और कोई undo नहीं देता -- यह call होते ही तुरंत और permanently delete कर देता है।

उदाहरण: Deleting a File with os.remove()

python
import os
with open("temp.txt", "w") as f:
    f.write("data")
os.remove("temp.txt")  # permanently deletes the file, no undo
print(os.path.exists("temp.txt"))

हटाने से पहले File का होना जाँचना

os.path.exists(path) True लौटाता है अगर उस path पर कोई file या folder मौजूद है, और os.remove() call करने से पहले इसे जाँचना पहले ही delete हो चुकी, कभी बनी ही न हो, या गलत नाम वाली file पर FileNotFoundError crash से बचाता है।

Note: जब file के मौजूद होने की गारंटी न हो, तो FileNotFoundError से script crash होने देने की बजाय हमेशा हटाने से पहले os.path.exists() जाँचें।
Warning: existence check छोड़ना और यह मान लेना कि file हमेशा मौजूद रहेगी, cleanup या maintenance scripts में unhandled crash का एक आम कारण है।

उदाहरण: Checking If a File Exists Before Deleting

python
import os
if os.path.exists("missing.txt"):  # avoids a crash if the file isn't there
    os.remove("missing.txt")
else:
    print("File does not exist")

खाली Folder हटाना

os.rmdir(path) किसी folder को हटाता है, पर सिर्फ़ तभी जब वह पूरी तरह खाली हो -- अभी भी files या subfolders वाले folder पर इसे call करना OSError raise करता है, यह एक safety measure है जो एक ही साधारण call से content से भरे folder को गलती से हटने से रोकता है।

Note: अब-खाली हो चुके folder पर os.rmdir() call करने से पहले पहले उसकी content खाली करें (हर file को अलग-अलग हटाकर)।
Warning: os.rmdir() का खाली न होने वाले folder को हटाने से मना करना एक जानबूझकर की गई safety feature है, bug नहीं -- यह खासतौर पर गलती से bulk deletion रोकने के लिए मौजूद है।

उदाहरण: Deleting an Empty Folder

python
import os
os.mkdir("empty_dir")
os.rmdir("empty_dir")  # only works because the folder is empty
print(os.path.exists("empty_dir"))

Folder और उसके अंदर सब कुछ हटाना

shutil.rmtree(path) पूरे folder tree को एक साथ हटा देता है, जिसमें उसके अंदर की हर file और subfolder शामिल है, चाहे उसमें कितनी भी content क्यों न हो -- यह os.rmdir() से कहीं ज़्यादा शक्तिशाली (और कहीं ज़्यादा खतरनाक) है, क्योंकि यह सब कुछ हटाने से पहले कभी confirmation नहीं माँगता।

Note: shutil.rmtree() को खासतौर पर उन folders के लिए इस्तेमाल करें जिनके बारे में आप निश्चित हैं कि उन्हें पूरी तरह और permanently हटाया जाना चाहिए -- इसकी शक्ति गलत path होने पर इरादे से ज़्यादा हटाना आसान बना देती है।
Warning: गलत path पर shutil.rmtree() (जैसे एक typo जो एक directory ऊपर point कर दे) इरादे से कहीं ज़्यादा data delete कर सकता है, और गलती पकड़ने के लिए कोई confirmation step नहीं होता।

उदाहरण: Deleting a Folder and Everything Inside It

python
import os
import shutil
os.makedirs("full_dir/sub", exist_ok=True)
with open("full_dir/file.txt", "w") as f:
    f.write("data")
shutil.rmtree("full_dir")  # deletes the whole folder tree, files and all
print(os.path.exists("full_dir"))

Safer Deletion Patterns

वाकई महत्वपूर्ण data के लिए, एक "soft delete" pattern (किसी file को वाकई हटाने की बजाय एक designated trash/archive folder में rename या move करना) वह safety net देता है जो immediate, permanent deletion नहीं देती -- इससे file को वापस move करके गलती को undo किया जा सकता है।

Note: जब भी हटाया जा रहा data वाकई बाद में मायने रख सकता हो, तो उसे वाकई हटाने की बजाय एक dedicated "deleted" या "archive" folder में move करने पर विचार करें।
Warning: सच्चा, तुरंत deletion (os.remove या shutil.rmtree) Python के अंदर कोई recovery रास्ता नहीं देता -- soft-delete pattern ही application स्तर पर safety net बनाने का इकलौता तरीका है।

उदाहरण: Safer Deletion Patterns

python
import os
import shutil
os.makedirs("archive", exist_ok=True)
with open("important.txt", "w") as f:
    f.write("data")
shutil.move("important.txt", "archive/important.txt")  # "soft delete" by moving instead of removing
print(os.path.exists("archive/important.txt"))
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.