PGDCA, BCA, B-Tech, MCA, M-Tech, B.Sc computer science राजस्थान सूचना सहायक व कंप्यूटर शिक्षक विशेष नोट्स: इस अध्याय में हम Writing Files: write(), writelines() and with Statement (Context Manager) (फाइल लेखन और 'with' स्टेटमेंट (ऑटो-क्लोज़ कॉन्टेक्स्ट मैनेजर)) की संपूर्ण थ्योरी, मुख्य तकनीकी बिंदु, तुलनात्मक तालिका और विगत परीक्षाओं के पैटर्न पर आधारित 20 अति-महत्वपूर्ण बहुविकल्पीय प्रश्न (MCQs) विस्तृत हिंदी व अंग्रेजी व्याख्या सहित पढ़ेंगे।
Overview: Writing files in Python relies on write() for single strings or bytes and writelines() for iterables, neither of which appends newlines automatically. Wrapping file operations inside a `with` statement uses the context manager protocol (__enter__/__exit__) to guarantee flushing and automatic closure even on exceptions.
अध्याय सार: पायथन में फाइल लिखने के लिए एकल स्ट्रिंग हेतु write() और इटरेबल हेतु writelines() उपयोग होता है, जिनमें से कोई भी स्वतः न्यूलाइन नहीं जोड़ता। `with` स्टेटमेंट कॉन्टेक्स्ट मैनेजर प्रोटोकॉल (__enter__/__exit__) का उपयोग कर त्रुटि आने पर भी फाइल का ऑटो-फ्लश और क्लोज़ सुनिश्चित करता है।
Writing Files: write(), writelines() and with Statement (Context Manager): Detailed Notes & Architecture (फाइल लेखन और 'with' स्टेटमेंट (ऑटो-क्लोज़ कॉन्टेक्स्ट मैनेजर))
1. write(s): Writing Strings or Bytes and Return Value (write(s): स्ट्रिंग/बाइट्स लिखना और रिटर्न वैल्यू)
[EN] `f.write(s)` writes a single `str` (in text mode) or `bytes`-like object (in binary mode) to the buffered stream and returns an `int` equal to the number of characters or bytes written. Unlike `print()`, `write()` does NOT append a `'\n'` newline automatically; consecutive calls `f.write('A'); f.write('B')` produce `'AB'` on a single line. Passing non-string types such as `f.write(42)` or `f.write(['a'])` raises `TypeError: write() argument must be str, not int`, requiring explicit `str(42)` conversion. Alternatively, `print('data', 42, file=f)` converts arguments via `str()` and appends `'\n'` automatically.
[HI] `f.write(s)` टेक्स्ट मोड में एक `str` (या बाइनरी मोड में `bytes`) लिखता है और सफलतापूर्वक लिखे गए कैरेक्टर्स/बाइट्स की संख्या (`int`) लौटाता है। `print()` के विपरीत, `write()` अंत में स्वतः `'\n'` नहीं जोड़ता, इसलिए लगातार दो कॉल `'AB'` को एक ही पंक्ति में लिख देती हैं। यदि `f.write(42)` जैसा पूर्णांक या लिस्ट दी जाए, तो `TypeError` आता है; इसलिए पहले `str(42)` में बदलना पड़ता है। सुविधाजनक विकल्प के रूप में `print('data', 42, file=f)` स्वतः स्ट्रिंग रूपांतरण और न्यूलाइन जोड़ देता है।
2. writelines(lines): Writing Iterables Without Implicit Newlines (writelines(lines): बिना स्वतः न्यूलाइन के इटरेबल लिखना)
[EN] `f.writelines(iterable)` accepts any iterable producing strings (in text mode) or bytes objects (in binary mode)—including a `list`, `tuple`, or memory-saving generator expression—and writes each element sequentially. A notorious exam trap is that `writelines()` does NOT insert `'\n'` separators between items and returns `None` (unlike `write()`, which returns an integer count). Calling `f.writelines(['line1', 'line2'])` writes `'line1line2'` on one line unless you include `'\n'` in each string or write `f.write('\n'.join(lines))`. If any element inside the iterable is not a `str` (e.g., `[1, 2]`), `writelines()` raises `TypeError`.
[HI] `f.writelines(iterable)` स्ट्रिंग्स उत्पन्न करने वाले किसी भी इटरेबल (जैसे `list`, `tuple` या जनरेटर एक्सप्रेशन) को क्रमिक रूप से फाइल में लिखता है। परीक्षा में अक्सर पूछा जाने वाला तथ्य यह है कि नाम के बावजूद `writelines()` आइटम्स के बीच स्वतः `'\n'` नहीं जोड़ता और रिटर्न वैल्यू `None` देता है। इसलिए `f.writelines(['line1', 'line2'])` से फाइल में `'line1line2'` लिखता है; अलग-अलग पंक्तियों के लिए प्रत्येक स्ट्रिंग में `'\n'` होना जरूरी है। इटरेबल में कोई गैर-स्ट्रिंग तत्व (जैसे `int`) होने पर `TypeError` उत्पन्न होता है।
3. The with Statement and Context Manager Protocol (with स्टेटमेंट और कॉन्टेक्स्ट मैनेजर प्रोटोकॉल)
[EN] Manually calling `f = open(...)` followed by `f.close()` risks leaking OS file descriptors or losing buffered writes if an exception occurs before `f.close()` executes. The `with open(...) as f:` construct invokes `f.__enter__()` on entry (binding the returned file object to `f`) and guarantees execution of `f.__exit__(exc_type, exc_val, exc_tb)` upon exiting the indented block. Even if an unhandled exception, `return`, or `break` exits the `with` block prematurely, `__exit__()` flushes buffers and closes the file automatically (`f.closed` becomes `True`). Note that `with` does NOT create a new variable scope: `f` remains accessible after the `with` block (as a closed file object), and multiple files can be opened in one `with open(a) as f1, open(b, 'w') as f2:` statement.
[HI] मैन्युअल `f.close()` लिखने पर यदि उससे पहले कोई एरर आ जाए, तो फाइल खुली रह जाती है और बफर डेटा डिस्क पर सेव नहीं हो पाता। `with open(...) as f:` ब्लॉक में प्रवेश पर `f.__enter__()` चलता है और ब्लॉक से बाहर निकलते समय (भले ही बीच में एक्सेप्शन आए या `return` हो) `f.__exit__()` का चलना 100% सुनिश्चित होता है। इससे फाइल स्वतः फ्लश और बंद हो जाती है (`f.closed == True`)। महत्वपूर्ण बात यह है कि `with` नया स्कोप नहीं बनाता, इसलिए ब्लॉक के बाहर भी `f` वेरिएबल (बंद फाइल के रूप में) बना रहता है; साथ ही एक ही `with` में कॉमा लगाकर कई फाइलें खोली जा सकती हैं।
4. User-Space Buffering, flush(), and close() Semantics (यूज़र-स्पेस बफरिंग, flush() और close() के नियम)
[EN] For I/O performance, Python buffers `write()` calls in user-space memory (commonly 4 KB or 8 KB chunks) rather than issuing an OS system call on every character. Consequently, written text may not appear on disk immediately while the file stays open unless `f.flush()` is called to push buffered bytes to the operating system. `f.close()` first flushes internal buffers and then releases the underlying OS file handle; calling `close()` twice is a safe no-op, whereas calling `f.write()` on a closed file raises `ValueError: I/O operation on closed file`. Passing `buffering=1` enables line buffering in text mode, while `buffering=0` is valid only in binary mode.
[HI] प्रदर्शन सुधारने के लिए पायथन `write()` किए गए डेटा को सीधे डिस्क पर लिखने के बजाय पहले RAM बफर (सामान्यतः 4 KB या 8 KB) में जमा करता है। फाइल खुले रहते हुए डेटा को तुरंत ऑपरेटिंग सिस्टम तक भेजने के लिए `f.flush()` प्रयुक्त होता है। `f.close()` पहले बफर को फ्लश करता है और फिर फाइल हैंडल मुक्त करता है; बंद हो चुकी फाइल पर दोबारा `write()` करने पर `ValueError: I/O operation on closed file` आता है। टेक्स्ट मोड में `buffering=1` लाइन-बफरिंग चालू करता है, जबकि अनबफर्ड `buffering=0` केवल बाइनरी मोड में ही मान्य है।
Syntax, Formula & Technical Example (Writing Files: write(), writelines() and with Statement (Context Manager))
lines = ["Row 1\n", "Row 2\n", "Row 3\n"]
with open("report.txt", "w", encoding="utf-8") as f:
chars = f.write("Header\n")
f.writelines(lines)
print("Header chars:", chars, "Inside closed?", f.closed)
print("Outside closed?", f.closed)
Writing Files: write(), writelines() and with Statement (Context Manager) Quick Reference & Comparison Table
| Write Method / Construct | Behavior & Return Value | Key Exam Fact |
|---|---|---|
| f.write(str) | Writes one str/bytes; returns int count | Does NOT auto-append '\n'; raises TypeError on int |
| f.writelines(iter) | Writes iterable items; returns None | Does NOT insert '\n' between items automatically |
| with open(...) as f: | Calls __enter__ and __exit__ cleanly | Auto-flushes & closes file even if exception raised |
| f.flush() vs f.close() | flush pushes RAM buffer; close frees FD | Writing after close() raises ValueError: closed file |
Writing Files: write(), writelines() and with Statement (Context Manager) Top 20 Bilingual MCQs with Answers
Q1. What does `f.write('Python')` return when writing to a text file?
(टेक्स्ट फाइल में `f.write('Python')` चलाने पर यह क्या लौटाता है?)
(A) True
(B) 6 (the integer number of characters written)
(C) None
(D) 'Python'
Correct Answer: (B)
[EN] In Python 3, `write()` returns the integer count of characters written in text mode (or bytes in binary mode).
[HI] पायथन 3 में `write()` सफलतापूर्वक लिखे गए कैरेक्टर्स (या बाइट्स) की संख्या लौटाता है, यहाँ `'Python'` के लिए 6।
Q2. If a list `data = ['A', 'B', 'C']` is passed to `f.writelines(data)` on a new file, what will the file contain?
(नई फाइल में `data = ['A', 'B', 'C']` के साथ `f.writelines(data)` चलाने पर फाइल के अंदर क्या लिखा जाएगा?)
(A) A B C
(B) A\nB\nC\n
(C) ['A', 'B', 'C']
(D) ABC
Correct Answer: (D)
[EN] writelines() concatenates the iterable items as-is without adding any newline characters between them.
[HI] writelines() बिना कोई न्यूलाइन जोड़े सभी स्ट्रिंग्स को लगातार जोड़कर `'ABC'` लिख देता है।
Q3. What is the return value of `f.writelines(['x\n', 'y\n'])`?
(`f.writelines(['x\n', 'y\n'])` की रिटर्न वैल्यू क्या होती है?)
(A) None
(B) True
(C) 2
(D) 4
Correct Answer: (A)
[EN] Unlike `write()` which returns an integer count, `writelines()` always returns `None`.
[HI] `write()` के विपरीत जो पूर्णांक संख्या लौटाता है, `writelines()` हमेशा `None` लौटाता है।
Q4. What exception is raised by calling `f.write(2026)` on a file opened in `'w'` mode?
(`'w'` मोड में खुली फाइल पर `f.write(2026)` कॉल करने पर कौन-सा एक्सेप्शन उत्पन्न होता है?)
(A) AttributeError
(B) TypeError
(C) ValueError
(D) SyntaxError
Correct Answer: (B)
[EN] `write()` requires a `str` in text mode (`bytes` in binary mode) and raises `TypeError` for integers.
[HI] टेक्स्ट मोड में `write()` केवल `str` स्वीकार करता है; `int` देने पर `TypeError` आता है।
Q5. Which pair of dunder methods defines Python's context manager protocol used by the `with` statement?
(`with` स्टेटमेंट में प्रयुक्त पायथन के कॉन्टेक्स्ट मैनेजर प्रोटोकॉल को किन दो डंडर मेथड्स से परिभाषित किया जाता है?)
(A) __init__ and __del__
(B) __enter__ and __exit__
(C) __open__ and __close__
(D) __start__ and __stop__
Correct Answer: (B)
[EN] Any object implementing `__enter__()` and `__exit__()` works as a context manager with `with`.
[HI] `__enter__()` और `__exit__()` मेथड्स कॉन्टेक्स्ट मैनेजर प्रोटोकॉल को पूरा करते हैं।
Q6. If a `ZeroDivisionError` occurs inside a `with open('log.txt', 'w') as f:` block right after `f.write('start')`, what happens to `f`?
(यदि `with open('log.txt', 'w') as f:` के अंदर `f.write('start')` के बाद `ZeroDivisionError` आ जाए, तो फाइल `f` का क्या होगा?)
(A) log.txt is deleted automatically
(B) f stays open and leaks until the OS kills the script
(C) The exception is silently swallowed by open()
(D) f is automatically flushed and closed before the exception propagates
Correct Answer: (D)
[EN] Python guarantees calling `f.__exit__()` even when an exception escapes the `with` block, closing the file safely.
[HI] एक्सेप्शन आने पर भी `with` ब्लॉक से बाहर निकलते समय `f.__exit__()` फाइल को फ्लश और बंद कर देता है।
Q7. Immediately after a `with open('a.txt', 'w') as f:` block finishes, what are `f.closed` and `f`?
(`with open('a.txt', 'w') as f:` ब्लॉक समाप्त होने के तुरंत बाद `f` और `f.closed` की स्थिति क्या होती है?)
(A) f is set to None
(B) f.closed is False until garbage collected
(C) NameError: f is deleted when the with block ends
(D) f is still in scope as a closed file object, and f.closed is True
Correct Answer: (D)
[EN] The `with` statement does not create a local scope, so `f` remains bound after the block with `f.closed == True`.
[HI] `with` नया स्कोप नहीं बनाता, इसलिए बाहर भी `f` उपलब्ध रहता है और `f.closed` का मान `True` होता है।
Q8. What exception is raised if you call `f.write('hi')` after `f.close()` has been executed?
(`f.close()` चलने के बाद उसी फाइल ऑब्जेक्ट पर `f.write('hi')` चलाने पर कौन-सा एक्सेप्शन आता है?)
(A) IOError: file locked
(B) ValueError: I/O operation on closed file
(C) EOFError
(D) NameError
Correct Answer: (B)
[EN] Attempting any read, write, flush, or seek on an already-closed file raises `ValueError`.
[HI] बंद हो चुकी फाइल पर कोई भी I/O ऑपरेशन करने पर `ValueError: I/O operation on closed file` आता है।
Q9. What does calling `f.flush()` on an open writable file accomplish?
(खुली हुई फाइल पर `f.flush()` कॉल करने से क्या होता है?)
(A) Erases all data in the file
(B) Forces buffered data in RAM to be written to the OS immediately without closing the file
(C) Closes the file and releases the descriptor
(D) Rewinds the file pointer back to byte 0
Correct Answer: (B)
[EN] `flush()` empties Python's internal write buffer into the OS kernel stream while keeping `f` open for more writes.
[HI] `flush()` फाइल को बंद किए बिना RAM बफर में रुके हुए डेटा को तुरंत ऑपरेटिंग सिस्टम तक भेज देता है।
Q10. How do you open two files safely in a single `with` statement in Python 3?
(पायथन 3 में एक ही `with` स्टेटमेंट के भीतर दो फाइलों को सुरक्षित रूप से कैसे खोला जाता है?)
(A) with open('in.txt') and open('out.txt', 'w') as fout:
(B) with open('in.txt') as fin, open('out.txt', 'w') as fout:
(C) It is invalid; two nested with blocks are mandatory
(D) with open('in.txt', 'out.txt') as fin, fout:
Correct Answer: (B)
[EN] Comma-separated context managers in a single `with` statement (and parenthesized multi-line `with (...)` in 3.10+) are fully supported.
[HI] एक ही `with` स्टेटमेंट में अल्पविराम (comma) से अलग करके कई फाइलें एक साथ खोली जा सकती हैं।
Q11. How does `print('Score:', 95, file=f)` differ from `f.write('Score: 95')`?
(`print('Score:', 95, file=f)` और `f.write('Score: 95')` में क्या अंतर है?)
(A) print() returns the byte count written while write() returns None
(B) print() only works in binary mode
(C) print() bypasses the with statement
(D) print() automatically converts non-strings via str() and appends '\n' by default
Correct Answer: (D)
[EN] `print(..., file=f)` handles space separation, `str()` conversion, and trailing newline automatically.
[HI] `print(..., file=f)` गैर-स्ट्रिंग मानों को स्वतः स्ट्रिंग में बदलता है और अंत में `'\n'` भी जोड़ देता है।
Q12. Which buffering value is allowed ONLY when opening a file in binary mode (`'wb'`) and raises `ValueError` in text mode?
(कौन-सा `buffering` मान केवल बाइनरी मोड में मान्य है और टेक्स्ट मोड में `ValueError` देता है?)
(A) buffering=-1
(B) buffering=8192
(C) buffering=0 (unbuffered I/O)
(D) buffering=1
Correct Answer: (C)
[EN] Unbuffered I/O (`buffering=0`) is permitted only for binary streams; text streams require buffering (`1` or positive chunk size).
[HI] अनबफर्ड मोड (`buffering=0`) केवल बाइनरी मोड में अनुमत है; टेक्स्ट मोड में देने पर `ValueError` आता है।
Q13. Can `f.writelines()` accept a generator expression like `(f'{i}\n' for i in range(3))`?
(क्या `f.writelines()` `(f'{i}\n' for i in range(3))` जैसे जनरेटर एक्सप्रेशन को स्वीकार कर सकता है?)
(A) Only in binary mode
(B) No, only a concrete list is accepted
(C) No, only lists and tuples are accepted
(D) Yes, because writelines() accepts any iterable yielding strings
Correct Answer: (D)
[EN] `writelines()` iterates over any iterable object lazily without requiring a list in memory.
[HI] हां, `writelines()` किसी भी स्ट्रिंग-उत्पादक इटरेबल (लिस्ट, टपल या जनरेटर) को स्वीकार करता है।
Q14. What happens if you call `f.close()` twice on the same file object?
(एक ही फाइल ऑब्जेक्ट पर दो बार `f.close()` कॉल करने पर क्या होता है?)
(A) The file on disk is deleted
(B) Nothing happens; the second close() is a safe no-op
(C) ValueError: file already closed
(D) OSError is raised
Correct Answer: (B)
[EN] Calling `close()` on an already closed file object safely does nothing without raising an error.
[HI] पहले से बंद फाइल पर दोबारा `f.close()` कॉल करने पर कोई एरर नहीं आता; यह सुरक्षित है।
Q15. Which method signature correctly represents `__exit__` in a custom context manager?
(कस्टम कॉन्टेक्स्ट मैनेजर में `__exit__` का सही मेथड सिग्नेचर कौन-सा है?)
(A) def __exit__(self, filename):
(B) def __exit__(self):
(C) def __exit__(self, return_code):
(D) def __exit__(self, exc_type, exc_val, exc_tb):
Correct Answer: (D)
[EN] Python passes exception type, value, and traceback (all `None` if no exception occurred) into `__exit__`.
[HI] पायथन ब्लॉक से निकलते समय `__exit__` को `exc_type, exc_val, exc_tb` तीन पैरामीटर भेजता है।
Q16. Which statement is NOT true about `write()`, `writelines()`, and `with`?
(`write()`, `writelines()` और `with` के बारे में निम्न में से कौन-सा कथन सत्य नहीं है?)
(A) f.writelines(['one', 'two']) automatically inserts '\n' after each list element
(B) f.write('abc') returns 3 in text mode
(C) with open(...) as f: closes the file even when an exception occurs
(D) f.writelines() returns None
Correct Answer: (A)
[EN] `writelines()` writes strings back-to-back without automatically adding `'\n'` between elements.
[HI] `writelines()` लिस्ट के तत्वों के बीच स्वतः `'\n'` नहीं जोड़ता; प्रत्येक स्ट्रिंग में `'\n'` स्वयं देना होता है।
Q17. Within Writing Files: write(), writelines() and with Statement (Context Manager), which write method / construct directly maps to: "Writes one str/bytes; returns int count"?
(Writing Files: write(), writelines() and with Statement (Context Manager) में कौन सा Write Method / Construct सीधे "Writes one str/bytes; returns int count" से संबंधित है?)
(A) f.write(str)
(B) f.flush() vs f.close()
(C) f.writelines(iter)
(D) with open(...) as f:
Correct Answer: (A)
[EN] In Writing Files: write(), writelines() and with Statement (Context Manager), 'f.write(str)' handles 'Writes one str/bytes; returns int count' (Does NOT auto-append '\n'; raises TypeError on int).
[HI] Writing Files: write(), writelines() and with Statement (Context Manager) में 'f.write(str)' का कार्य/विवरण 'Writes one str/bytes; returns int count' (Does NOT auto-append '\n'; raises TypeError on int) है।
Q18. Which option accurately describes 'f.writelines(iter)' in Writing Files: write(), writelines() and with Statement (Context Manager)?
(Writing Files: write(), writelines() and with Statement (Context Manager) के संदर्भ में 'f.writelines(iter)' की सही विशेषता कौन सी है?)
(A) Writes iterable items; returns None
(B) Calls __enter__ and __exit__ cleanly
(C) flush pushes RAM buffer; close frees FD
(D) Writes one str/bytes; returns int count
Correct Answer: (A)
[EN] 'f.writelines(iter)' provides 'Writes iterable items; returns None' — key point: Does NOT insert '\n' between items automatically.
[HI] 'f.writelines(iter)' का मुख्य विवरण 'Writes iterable items; returns None' है (Does NOT insert '\n' between items automatically)।
Q19. In Writing Files: write(), writelines() and with Statement (Context Manager), identify the correct parameter for the specification: "Calls __enter__ and __exit__ cleanly".
(Writing Files: write(), writelines() and with Statement (Context Manager) में "Calls __enter__ and __exit__ cleanly" विवरण किस विकल्प के लिए सही है?)
(A) with open(...) as f:
(B) f.flush() vs f.close()
(C) f.write(str)
(D) f.writelines(iter)
Correct Answer: (A)
[EN] 'with open(...) as f:' corresponds to 'Calls __enter__ and __exit__ cleanly' (Auto-flushes & closes file even if exception raised).
[HI] 'with open(...) as f:' का संबंध 'Calls __enter__ and __exit__ cleanly' (Auto-flushes & closes file even if exception raised) से है।
Q20. Considering Writing Files: write(), writelines() and with Statement (Context Manager), what key behavior defines 'f.flush() vs f.close()'?
(Writing Files: write(), writelines() and with Statement (Context Manager) में 'f.flush() vs f.close()' की मुख्य भूमिका या मान क्या है?)
(A) flush pushes RAM buffer; close frees FD
(B) Calls __enter__ and __exit__ cleanly
(C) Writes iterable items; returns None
(D) Writes one str/bytes; returns int count
Correct Answer: (A)
[EN] 'f.flush() vs f.close()' represents 'flush pushes RAM buffer; close frees FD' (Writing after close() raises ValueError: closed file).
[HI] 'f.flush() vs f.close()' का अर्थ/मान 'flush pushes RAM buffer; close frees FD' है (Writing after close() raises ValueError: closed file)।
Writing Files: write(), writelines() and with Statement (Context Manager) — Exam FAQs
Q1: Does a `with open(...) as f:` statement create a new variable scope?
क्या `with open(...) as f:` स्टेटमेंट अपना नया वेरिएबल स्कोप बनाता है?
[EN] No. Just like `if` and `for`, `with` shares the surrounding function or module scope. Variables created inside the `with` block—and `f` itself—remain accessible after the block finishes (with `f` in a closed state).
[HI] नहीं, `if` और `for` की तरह `with` भी नया स्कोप नहीं बनाता। `with` के भीतर बने वेरिएबल और स्वयं `f` ब्लॉक समाप्त होने के बाद भी स्कोप में रहते हैं (बस फाइल बंद हो चुकी होती है)।
Q2: Why did Python name the method `writelines()` if it does not add newlines automatically?
यदि `writelines()` स्वतः न्यूलाइन नहीं जोड़ता, तो पायथन ने इसका नाम `writelines()` क्यों रखा?
[EN] It acts as the exact symmetric counterpart to `readlines()`. Because `readlines()` preserves `'\n'` on every string in the list it returns, passing that exact list straight back into `writelines()` reproduces the original file without adding extra duplicate newlines.
[HI] यह `readlines()` का ठीक विपरीत जोड़ीदार है। चूंकि `readlines()` प्रत्येक पंक्ति के साथ `'\n'` को सुरक्षित रखता है, इसलिए उसी लिस्ट को सीधे `writelines()` में देने पर फाइल बिल्कुल मूल रूप में बन जाती है।
Q3: What happens if a custom `__exit__` method returns `True`?
यदि किसी कस्टम कॉन्टेक्स्ट मैनेजर का `__exit__` मेथड `True` लौटाए तो क्या होता है?
[EN] Returning a truthy value (`True`) from `__exit__` tells Python to suppress any exception that was raised inside the `with` block, allowing execution to continue cleanly right after the block.
[HI] `__exit__` द्वारा `True` लौटाने पर पायथन `with` ब्लॉक के भीतर आए एक्सेप्शन को दबा देता है (suppress कर देता है) और प्रोग्राम ब्लॉक के बाहर सामान्य रूप से आगे बढ़ता है।
Post a Comment
Post a Comment