File Handling: open() Modes (r, w, a, r+, w+, rb, wb) Notes in Hindi & English : फाइल हैंडलिंग: open() मोड्स (टेक्स्ट बनाम बाइनरी मोड्स) और 20 महत्वपूर्ण MCQ प्रश्न (Rajasthan IT Exam)

Post a Comment

PGDCA, BCA, B-Tech, MCA, M-Tech, B.Sc computer science राजस्थान सूचना सहायक व कंप्यूटर शिक्षक विशेष नोट्स: इस अध्याय में हम File Handling: open() Modes (r, w, a, r+, w+, rb, wb) (फाइल हैंडलिंग: open() मोड्स (टेक्स्ट बनाम बाइनरी मोड्स)) की संपूर्ण थ्योरी, मुख्य तकनीकी बिंदु, तुलनात्मक तालिका और विगत परीक्षाओं के पैटर्न पर आधारित 20 अति-महत्वपूर्ण बहुविकल्पीय प्रश्न (MCQs) विस्तृत हिंदी व अंग्रेजी व्याख्या सहित पढ़ेंगे।

Overview: Python's built-in open() function opens files in text or binary streams using modes such as r, w, a, x, r+, w+, rb, and wb. Each mode dictates whether missing files trigger errors or get created, whether existing data is truncated, and where the initial cursor points.

अध्याय सार: पायथन का बिल्ट-इन open() फंक्शन r, w, a, x, r+, w+, rb और wb जैसे मोड्स द्वारा टेक्स्ट या बाइनरी फाइल स्ट्रीम खोलता है। प्रत्येक मोड यह तय करता है कि फाइल न मिलने पर एरर आएगा या नई फाइल बनेगी, पुराना डेटा मिटेगा या बचेगा, और फाइल पॉइंटर कहां स्थित होगा।

File Handling: open() Modes (r, w, a, r+, w+, rb, wb): Detailed Notes & Architecture (फाइल हैंडलिंग: open() मोड्स (टेक्स्ट बनाम बाइनरी मोड्स))

1. Anatomy of open(), Default Mode, and Text Encoding (open() की संरचना, डिफ़ॉल्ट मोड और टेक्स्ट एन्कोडिंग)

[EN] The signature `open(file, mode='r', encoding=None)` defaults to `'r'` (equivalent to `'rt'`, read-only text mode) and returns an `io.TextIOWrapper` instance. In text mode, Python decodes bytes into `str` objects and automatically translates platform-specific line endings (`\r\n` on Windows into `\n` when reading, and back on writing). If `encoding` is omitted in text mode, Python relies on the operating system's locale encoding (e.g., `cp1252` on Windows vs `utf-8` on Linux), which frequently triggers `UnicodeDecodeError` on portable files. Best practice per PEP 597 is to pass `encoding='utf-8'` explicitly whenever opening text files.

[HI] `open(file, mode='r', encoding=None)` का डिफ़ॉल्ट मोड `'r'` (अर्थात `'rt'`, रीड टेक्स्ट मोड) होता है और यह `io.TextIOWrapper` ऑब्जेक्ट लौटाता है। टेक्स्ट मोड में पायथन बाइट्स को `str` में डिकोड करता है और ऑपरेटिंग सिस्टम के लाइन एंडिंग (`\r\n` को `\n` में) को स्वतः रूपांतरित करता है। यदि `encoding` न दिया जाए, तो पायथन सिस्टम की डिफ़ॉल्ट एन्कोडिंग (जैसे Windows पर `cp1252`) का उपयोग करता है जिससे पोर्टेबिलिटी में `UnicodeDecodeError` आ सकता है। इसलिए टेक्स्ट फाइलें खोलते समय हमेशा `encoding='utf-8'` स्पष्ट रूप से लिखना सर्वोत्तम माना जाता है।

2. Primary Modes: r, w, a, and Exclusive x (प्राथमिक मोड्स: r, w, a और एक्सक्लूसिव x)

[EN] Read mode `'r'` positions the cursor at byte 0 and raises `FileNotFoundError` if the target file does not exist; writing to it raises `io.UnsupportedOperation`. Write mode `'w'` creates a new file if absent, or immediately truncates an existing file to zero bytes the moment `open()` executes—erasing prior contents before any `write()` call. Append mode `'a'` preserves existing contents, creates the file if missing, and forces every write operation to the end of the file (EOF). Exclusive creation mode `'x'` (Python 3.3+) succeeds only when creating a brand-new file and raises `FileExistsError` if the path already exists, preventing accidental overwrites.

[HI] रीड मोड `'r'` पॉइंटर को 0 पर रखता है और फाइल न होने पर `FileNotFoundError` देता है; इसमें लिखने का प्रयास करने पर त्रुटि आती है। राइट मोड `'w'` फाइल न होने पर नई फाइल बनाता है, लेकिन फाइल पहले से होने पर `open()` होते ही उसका सारा पुराना डेटा तुरंत मिटाकर (truncate) 0 बाइट कर देता है। अपेंड मोड `'a'` पुराना डेटा सुरक्षित रखता है और सभी नई प्रविष्टियाँ फाइल के अंत (EOF) में जोड़ता है। एक्सक्लूसिव मोड `'x'` केवल नई फाइल बनाने के लिए है और फाइल पहले से मौजूद होने पर `FileExistsError` उत्पन्न कर ओवरराइटिंग से बचाता है।

3. Combined Read-Write Modes: r+, w+, and a+ (संयुक्त रीड-राइट मोड्स: r+, w+ और a+)

[EN] Adding `+` enables both reading and writing on the same stream, but the modes differ dramatically in startup behavior. `'r+'` requires the file to already exist (`FileNotFoundError` otherwise), preserves contents, and starts the cursor at offset 0 so writes overwrite bytes in place from the current position. `'w+'` creates or truncates the file to empty immediately on open, requiring `f.seek(0)` after writing if you wish to read back what you just wrote. `'a+'` creates the file if needed and allows seeking backward to read, yet every write automatically jumps to EOF regardless of prior `seek()` calls.

[HI] `+` चिन्ह जोड़ने से एक ही फाइल हैंडल पर पढ़ना और लिखना दोनों संभव हो जाता है, परंतु इनके शुरुआती नियम अलग हैं। `'r+'` में फाइल का पहले से मौजूद होना जरूरी है; यह पुराना डेटा नहीं मिटाता और पॉइंटर 0 पर रखकर मौजूदा बाइट्स को ओवरराइट करता है। `'w+'` खुलते ही पुरानी फाइल का डेटा शून्य कर देता है; इसमें लिखने के बाद पढ़ने के लिए `f.seek(0)` करना पड़ता है। `'a+'` में `seek(0)` से पढ़ा तो जा सकता है, लेकिन हर बार लिखते समय पॉइंटर स्वतः फाइल के अंत (EOF) पर चला जाता है।

4. Text Mode vs Binary Mode: rb, wb, ab, and Buffering (टेक्स्ट बनाम बाइनरी मोड: rb, wb, ab और बफरिंग)

[EN] Appending `'b'` (`'rb'`, `'wb'`, `'ab'`, `'rb+'`) opens the file in unencoded binary mode, returning `BufferedReader`, `BufferedWriter`, or `BufferedRandom` objects. Binary mode performs zero character decoding and disables `\r\n` newline translation, reading and writing exact `bytes` objects instead of `str`. Binary mode is mandatory for images, audio, PDFs, ZIP archives, and `pickle` serialization; opening such files in text mode corrupts data or fails decoding. Passing an `encoding` argument together with binary mode (`'rb'` or `'wb'`) raises `ValueError: binary mode doesn't take an encoding argument`.

[HI] `'b'` (`'rb'`, `'wb'`, `'ab'`) जोड़ने पर फाइल बाइनरी मोड में खुलती है, जिसमें अक्षर डिकोडिंग और न्यूलाइन रूपांतरण (`\r\n`) बिल्कुल बंद रहता है। बाइनरी मोड में `str` के बजाय शुद्ध `bytes` ऑब्जेक्ट पढ़े और लिखे जाते हैं। इमेज, ऑडियो, PDF, ZIP और `pickle` फाइलों के लिए बाइनरी मोड अनिवार्य है; टेक्स्ट मोड में खोलने पर डेटा खराब हो सकता है। बाइनरी मोड (`'rb'` या `'wb'`) के साथ `encoding='utf-8'` पास करने पर `ValueError` उत्पन्न होता है।

Syntax, Formula & Technical Example (File Handling: open() Modes (r, w, a, r+, w+, rb, wb))

with open("notes.txt", "w+", encoding="utf-8") as f:
    f.write("RSMSSB Python\n")
    print("Pos after write:", f.tell())
    f.seek(0)
    print("Read back:", f.read().strip())
with open("notes.txt", "rb") as bf:
    raw = bf.read()
    print(type(raw), raw)

File Handling: open() Modes (r, w, a, r+, w+, rb, wb) Quick Reference & Comparison Table

open() ModeMissing / Existing File BehaviorKey Exam Fact
'r' / 'rb'FileNotFoundError if missing; preserves dataDefault mode is 'r' (text read); cursor starts at 0
'w' / 'wb'Creates new; truncates existing to 0 bytesErases existing content immediately when open() runs
'a' / 'ab'Creates new; keeps existing content intactAll writes always append strictly at end of file
'r+' vs 'w+''r+' fails if missing; 'w+' truncates firstBoth allow read & write; use seek(0) before re-reading

File Handling: open() Modes (r, w, a, r+, w+, rb, wb) Top 20 Bilingual MCQs with Answers

Q1. What is the default mode when calling `open('data.txt')` without specifying a second argument?

(दूसरा आर्ग्यूमेंट दिए बिना `open('data.txt')` कॉल करने पर डिफ़ॉल्ट मोड क्या होता है?)

(A) 'w' (write mode)

(B) 'r+' (read and write mode)

(C) 'r' (read-only text mode)

(D) 'rb' (read-only binary mode)

Correct Answer: (C)

[EN] Python's open() defaults to mode='r', which opens a file for reading in text mode ('rt').

[HI] पायथन में open() का डिफ़ॉल्ट मोड `'r'` (रीड-ओनली टेक्स्ट मोड) होता है।

Q2. Which file mode immediately erases all existing contents of a file as soon as `open()` is called?

(`open()` कॉल होते ही कौन-सा फाइल मोड पहले से मौजूद फाइल का सारा डेटा तुरंत मिटा देता है?)

(A) 'w' (and 'w+')

(B) 'x'

(C) 'r+'

(D) 'a'

Correct Answer: (A)

[EN] Opening an existing file with 'w' or 'w+' truncates its size to 0 bytes immediately upon opening.

[HI] 'w' और 'w+' मोड खुलते ही पहले से मौजूद फाइल के पूरे डेटा को 0 बाइट कर देते हैं।

Q3. Which exception is raised when attempting `open('missing.txt', 'r+')` on a file that does not exist?

(यदि `missing.txt` मौजूद नहीं है और `open('missing.txt', 'r+')` चलाया जाए, तो कौन-सा एक्सेप्शन आएगा?)

(A) EOFError

(B) FileExistsError

(C) PermissionError

(D) FileNotFoundError

Correct Answer: (D)

[EN] Mode 'r+' requires the file to exist beforehand and raises FileNotFoundError if it is missing.

[HI] 'r+' मोड के लिए फाइल का पहले से होना आवश्यक है; फाइल न मिलने पर FileNotFoundError आता है।

Q4. Which mode raises `FileExistsError` if the target file already exists on disk?

(यदि लक्ष्य फाइल डिस्क पर पहले से मौजूद हो, तो कौन-सा मोड `FileExistsError` देता है?)

(A) 'x'

(B) 'a'

(C) 'w'

(D) 'w+'

Correct Answer: (A)

[EN] Exclusive creation mode 'x' protects existing files by failing with FileExistsError if the path exists.

[HI] एक्सक्लूसिव क्रिएशन मोड `'x'` पहले से मौजूद फाइल को ओवरराइट होने से बचाता है और FileExistsError देता है।

Q5. What data type does `f.read()` return when a file is opened in `'rb'` mode?

(जब फाइल `'rb'` मोड में खोली जाती है, तो `f.read()` किस डेटा टाइप में मान लौटाता है?)

(A) bytearray

(B) list

(C) str

(D) bytes

Correct Answer: (D)

[EN] Binary modes ('rb', 'wb', 'ab') skip UTF-8 decoding and return immutable `bytes` objects.

[HI] बाइनरी मोड (`'rb'`) में फाइल से `bytes` ऑब्जेक्ट लौटता है, `str` नहीं।

Q6. What happens if you pass `encoding='utf-8'` to `open('img.png', 'rb', encoding='utf-8')`?

(`open('img.png', 'rb', encoding='utf-8')` लिखने पर क्या होता है?)

(A) It ignores encoding silently

(B) ValueError: binary mode doesn't take an encoding argument

(C) It converts the PNG image into UTF-8 text

(D) TypeError: unexpected keyword argument

Correct Answer: (B)

[EN] Binary streams handle raw bytes without character encoding, so passing `encoding` raises ValueError.

[HI] बाइनरी मोड में कैरेक्टर एन्कोडिंग लागू नहीं होती, इसलिए `encoding` देने पर `ValueError` आता है।

Q7. What happens if you call `f.write('abc')` on a file opened in `'rb'` or `'r'` mode?

(`'r'` या `'rb'` मोड में खुली फाइल पर `f.write('abc')` कॉल करने पर क्या होता है?)

(A) It silently appends 'abc' to the end

(B) FileNotFoundError

(C) EOFError

(D) io.UnsupportedOperation (not writable)

Correct Answer: (D)

[EN] Writing to a read-only stream raises `io.UnsupportedOperation: not writable`.

[HI] रीड-ओनली मोड में खुली फाइल पर लिखने का प्रयास करने पर `io.UnsupportedOperation` त्रुटि आती है।

Q8. In `'a+'` mode on a non-empty file, if you call `f.seek(0)` and then `f.write('END')`, where is `'END'` written?

(गैर-खाली फाइल को `'a+'` मोड में खोलकर `f.seek(0)` के बाद `f.write('END')` चलाने पर `'END'` कहां लिखा जाएगा?)

(A) In the middle of the file

(B) At the beginning of the file overwriting existing bytes

(C) It raises io.UnsupportedOperation

(D) At the very end of the file (EOF)

Correct Answer: (D)

[EN] Append modes ('a', 'a+', 'ab') guarantee all writes go strictly to the end of the file, ignoring seek() for writes.

[HI] अपेंड मोड में हर लेखन कार्य `seek(0)` के बावजूद हमेशा फाइल के अंत (EOF) में ही होता है।

Q9. Which method returns the current integer offset of the file stream cursor?

(फाइल स्ट्रीम कर्सर की वर्तमान स्थिति (ऑफसेट) किस मेथड से जानी जाती है?)

(A) f.cursor()

(B) f.pos()

(C) f.tell()

(D) f.seek()

Correct Answer: (C)

[EN] `f.tell()` returns the current stream position, while `f.seek(offset)` moves it.

[HI] `f.tell()` फाइल पॉइंटर की वर्तमान स्थिति बताता है, जबकि `f.seek()` पॉइंटर को आगे-पीछे ले जाता है।

Q10. After writing data in `'w+'` mode, why does an immediate `f.read()` return `''`?

(`'w+'` मोड में डेटा लिखने के तुरंत बाद `f.read()` चलाने पर खाली स्ट्रिंग `''` क्यों मिलती है?)

(A) Because the file pointer is sitting at the end of the newly written text; you must call f.seek(0) first

(B) Because 'w+' does not allow reading

(C) Because text is deleted after write()

(D) Because write() automatically closes the stream

Correct Answer: (A)

[EN] Writing advances the file pointer to EOF, so reading without rewinding via `f.seek(0)` finds nothing ahead.

[HI] लिखने के बाद फाइल पॉइंटर अंत (EOF) पर पहुंच जाता है, इसलिए दोबारा पढ़ने से पहले `f.seek(0)` से शुरुआत में आना पड़ता है।

Q11. What type of object is returned by `open('log.txt', 'r', encoding='utf-8')`?

(`open('log.txt', 'r', encoding='utf-8')` किस क्लास का ऑब्जेक्ट लौटाता है?)

(A) io.TextIOWrapper

(B) str

(C) os.PathLike

(D) io.BufferedReader

Correct Answer: (A)

[EN] Opening a file in text mode wraps a binary buffer inside an `io.TextIOWrapper`.

[HI] टेक्स्ट मोड में फाइल खोलने पर `io.TextIOWrapper` ऑब्जेक्ट लौटता है।

Q12. What does text mode automatically do to Windows `\r\n` line endings during `read()` unless `newline=''` is set?

(यदि `newline=''` न दिया जाए, तो टेक्स्ट मोड Windows के `\r\n` लाइन एंडिंग को `read()` करते समय क्या करता है?)

(A) Translates them into universal '\n'

(B) Raises UnicodeDecodeError

(C) Leaves them unchanged as '\r\n'

(D) Strips all newlines completely

Correct Answer: (A)

[EN] Universal newline translation in Python text mode normalizes `\r\n` and `\r` into `\n` on read.

[HI] पायथन का टेक्स्ट मोड पढ़ते समय `\r\n` को मानक `'\n'` में बदल देता है।

Q13. Which mode combination should be used to read a `.jpg` image file and copy its raw bytes to another file?

(किसी `.jpg` इमेज फाइल को पढ़कर उसकी रॉ बाइट्स को दूसरी फाइल में कॉपी करने के लिए कौन-से मोड उपयोग होंगे?)

(A) 'r' for reading and 'w' for writing

(B) 'rb' for reading and 'wb' for writing

(C) 'a' for reading and 'x' for writing

(D) 'rt' for reading and 'wt' for writing

Correct Answer: (B)

[EN] Images are binary files and must be opened in binary modes `'rb'` and `'wb'` to avoid encoding errors and byte corruption.

[HI] इमेज जैसी बाइनरी फाइलों के लिए हमेशा `'rb'` और `'wb'` मोड का उपयोग किया जाता है।

Q14. What is the difference between `'r+'` and `'w+'` on an existing file containing `'HELLO'` if you immediately write `'HI'`?

(यदि फाइल में पहले से `'HELLO'` लिखा है, तो `'r+'` और `'w+'` में खोलकर तुरंत `'HI'` लिखने पर फाइल में क्या बचेगा?)

(A) 'r+' produces 'HELLO HI', whereas 'w+' produces 'HI'

(B) Both produce 'HI'

(C) 'r+' produces 'HILLO', whereas 'w+' produces 'HI'

(D) Both produce 'HILLO'

Correct Answer: (C)

[EN] 'r+' overwrites only the first 2 bytes leaving 'LLO', whereas 'w+' erased the whole file upon opening leaving only 'HI'.

[HI] 'r+' पुराना डेटा बचाते हुए शुरू के 2 अक्षर बदलकर `'HILLO'` कर देता है, जबकि `'w+'` पूरा पुराना डेटा मिटाकर केवल `'HI'` रखता है।

Q15. What does `f.seek(0, 2)` do on a file opened in binary mode `'rb'`?

(बाइनरी मोड `'rb'` में खुली फाइल पर `f.seek(0, 2)` क्या करता है?)

(A) Moves the pointer to line 2

(B) Rewinds 2 bytes from the start

(C) Closes the file after 2 seconds

(D) Moves the file pointer 0 bytes from the end of the file (to EOF)

Correct Answer: (D)

[EN] In `seek(offset, whence)`, `whence=0` is file start, `1` is current position, and `2` is end of file (EOF).

[HI] `seek(0, 2)` में `whence=2` का अर्थ फाइल का अंत (EOF) होता है, जिससे पॉइंटर फाइल के अंत में चला जाता है।

Q16. Which statement is NOT true about Python file `open()` modes?

(पायथन फाइल `open()` मोड्स के बारे में निम्न में से कौन-सा कथन सत्य नहीं है?)

(A) 'a' mode creates a new file if it does not already exist

(B) Omitting the mode argument defaults to 'r' (text read)

(C) 'rb' mode reads raw bytes without newline translation

(D) Opening a non-empty file in 'w' mode preserves old data and appends new text at the start

Correct Answer: (D)

[EN] 'w' mode truncates existing contents to zero bytes immediately upon opening; it does not preserve old data.

[HI] 'w' मोड खुलते ही पुराने डेटा को पूरी तरह मिटा (truncate) देता है, उसे सुरक्षित नहीं रखता।

Q17. Within File Handling: open() Modes (r, w, a, r+, w+, rb, wb), which open() mode directly maps to: "FileNotFoundError if missing; preserves data"?

(File Handling: open() Modes (r, w, a, r+, w+, rb, wb) में कौन सा open() Mode सीधे "FileNotFoundError if missing; preserves data" से संबंधित है?)

(A) 'a' / 'ab'

(B) 'w' / 'wb'

(C) 'r' / 'rb'

(D) 'r+' vs 'w+'

Correct Answer: (C)

[EN] In File Handling: open() Modes (r, w, a, r+, w+, rb, wb), ''r' / 'rb'' handles 'FileNotFoundError if missing; preserves data' (Default mode is 'r' (text read); cursor starts at 0).

[HI] File Handling: open() Modes (r, w, a, r+, w+, rb, wb) में ''r' / 'rb'' का कार्य/विवरण 'FileNotFoundError if missing; preserves data' (Default mode is 'r' (text read); cursor starts at 0) है।

Q18. Which option accurately describes ''w' / 'wb'' in File Handling: open() Modes (r, w, a, r+, w+, rb, wb)?

(File Handling: open() Modes (r, w, a, r+, w+, rb, wb) के संदर्भ में ''w' / 'wb'' की सही विशेषता कौन सी है?)

(A) 'r+' fails if missing; 'w+' truncates first

(B) Creates new; truncates existing to 0 bytes

(C) FileNotFoundError if missing; preserves data

(D) Creates new; keeps existing content intact

Correct Answer: (B)

[EN] ''w' / 'wb'' provides 'Creates new; truncates existing to 0 bytes' — key point: Erases existing content immediately when open() runs.

[HI] ''w' / 'wb'' का मुख्य विवरण 'Creates new; truncates existing to 0 bytes' है (Erases existing content immediately when open() runs)।

Q19. In File Handling: open() Modes (r, w, a, r+, w+, rb, wb), identify the correct parameter for the specification: "Creates new; keeps existing content intact".

(File Handling: open() Modes (r, w, a, r+, w+, rb, wb) में "Creates new; keeps existing content intact" विवरण किस विकल्प के लिए सही है?)

(A) 'w' / 'wb'

(B) 'r+' vs 'w+'

(C) 'a' / 'ab'

(D) 'r' / 'rb'

Correct Answer: (C)

[EN] ''a' / 'ab'' corresponds to 'Creates new; keeps existing content intact' (All writes always append strictly at end of file).

[HI] ''a' / 'ab'' का संबंध 'Creates new; keeps existing content intact' (All writes always append strictly at end of file) से है।

Q20. Considering File Handling: open() Modes (r, w, a, r+, w+, rb, wb), what key behavior defines ''r+' vs 'w+''?

(File Handling: open() Modes (r, w, a, r+, w+, rb, wb) में ''r+' vs 'w+'' की मुख्य भूमिका या मान क्या है?)

(A) Creates new; truncates existing to 0 bytes

(B) FileNotFoundError if missing; preserves data

(C) 'r+' fails if missing; 'w+' truncates first

(D) Creates new; keeps existing content intact

Correct Answer: (C)

[EN] ''r+' vs 'w+'' represents ''r+' fails if missing; 'w+' truncates first' (Both allow read & write; use seek(0) before re-reading).

[HI] ''r+' vs 'w+'' का अर्थ/मान ''r+' fails if missing; 'w+' truncates first' है (Both allow read & write; use seek(0) before re-reading)।

File Handling: open() Modes (r, w, a, r+, w+, rb, wb) — Exam FAQs

Q1: What is the practical difference between `'r+'` and `'w+'` if both allow reading and writing?

यदि `'r+'` और `'w+'` दोनों पढ़ने और लिखने की अनुमति देते हैं, तो दोनों में व्यावहारिक अंतर क्या है?

[EN] `'r+'` fails if the file does not exist and keeps existing content intact, letting you edit bytes in place from position 0. `'w+'` creates the file if missing and immediately wipes out any existing content on open, useful for writing fresh output and reading it back.

[HI] `'r+'` के लिए फाइल का पहले से होना जरूरी है और वह पुराने डेटा को सुरक्षित रखता है। इसके विपरीत `'w+'` फाइल न होने पर बना देता है लेकिन पुरानी फाइल का सारा डेटा तुरंत मिटा देता है।

Q2: Why does opening a binary file like a PDF or image in `'r'` text mode often crash?

PDF या इमेज जैसी बाइनरी फाइल को `'r'` टेक्स्ट मोड में खोलने पर प्रोग्राम क्रैश क्यों हो जाता है?

[EN] Text mode tries to decode arbitrary binary bytes using UTF-8 or locale rules, quickly hitting invalid byte sequences that trigger `UnicodeDecodeError`, and may corrupt byte sequences resembling `\r\n`.

[HI] टेक्स्ट मोड बाइनरी बाइट्स को UTF-8 अक्षरों में डिकोड करने का प्रयास करता है, जिससे अवैध बाइट मिलने पर `UnicodeDecodeError` आ जाता है या `\r\n` जैसा डेटा बदल जाता है।

Q3: Why is `newline=''` passed when opening CSV files with Python's `csv` module?

पायथन के `csv` मॉड्यूल के साथ फाइल खोलते समय `newline=''` क्यों पास किया जाता है?

[EN] The `csv` module handles its own embedded newlines inside quoted fields. Passing `newline=''` disables `open()`'s automatic `\r\n` translation so Windows does not end up writing blank lines between every CSV row.

[HI] `csv` मॉड्यूल स्वयं लाइन एंडिंग संभालता है। `newline=''` देने से `open()` का स्वतः `\r\n` रूपांतरण बंद रहता है और Windows पर हर पंक्ति के बीच खाली लाइन आने की समस्या नहीं होती।

Related Posts

  • Loading related posts...

Post a Comment

Search Topic Notes & 20 MCQs (Hindi / English)