Reading a Specific Number of Characters (निश्चित Characters पढ़ना) :-
Python में read() Method के अंदर Number देकर File से निश्चित संख्या में Characters Read किए जा सकते हैं।
English: Reading a Specific Number of Characters means reading only the specified number of characters from a file using the read(size) method.
Syntax: file.read(number)
यहाँ number उन Characters की संख्या बताता है जिन्हें Read करना है।
Example 1: पहले 5 Characters पढ़ना
Explanation:
read(5) File से पहले 5 Characters Read करता है।
इसमें H, e, l, l, o शामिल हैं।
बाकी Data Read नहीं होता।
Example 2: अलग-अलग बार Characters Read करना
Explanation:
पहला read(5) पाँच Characters Read करता है।
File Pointer पाँच Characters आगे चला जाता है।
दूसरा read(6) अगली छह Characters Read करता है।
इसलिए दूसरी बार Reading वहीं से शुरू होती है जहाँ पहली बार समाप्त हुई थी।
Example 3: उपलब्ध Characters से अधिक Number देना
Explanation: यदि माँगे गए Characters की संख्या File में उपलब्ध Characters से अधिक है, तो read() केवल उपलब्ध Data Read करता है।
Important Points:
read(5) का अर्थ है 5 Characters Read करना।
Text Mode में Size Characters की संख्या बताता है।
read() के बाद File Pointer आगे बढ़ जाता है।
File के अंत तक पहुँचने पर आगे read() करने पर Empty String मिलती है।