Reading File in Python Line by Line

Reading File in Python Line by Line – हालांकि पिछले Example में हमने read() Method का प्रयोग करते हुए File के Content को Read किया है, लेकिन File के Content को Read करने के लिए File Object हमें और भी कई बेहतर Methods Provide करता है।

उदाहरण के‍ लिए read() Method, Options Parameter के रूप में एक Number Accept करता है और एक बार में Specified Number के बराबर Characters / Bytes Read करके Return कर देता है, जबकि readline() Method एक बार में File की पूरी एक Line Read करके Return कर देता है। इसी तरह से और भी कई Methods हैं, जो अलग-अलग तरह की जरूरतों को पूरा करने के लिए Use किए जाते हैं।

इसी तरह से जब हमें हमारी File में किसी Specific Content की Position को Search करना होता है, तब हम seek() Method Use करते हैं, जो कि हर Searching के बाद अगले File Position पर Move हो जाता है। जबकि किसी File के Content को Read करने के Best तरीके के रूप में हम File Object के Iterator को Use कर सकते हैं और एक for…in Loop के माध्‍यम से हर Iteration में File की एक Single Line को Read कर सकते हैं। जैसे-

[code]
FileName: FileIterator.py
# Create a New File in Writing Mode using 'w'
filePointer = open("names.txt", "r")

# Display all Supported Attributes of the File Object
for line in filePointer:
    print(line, end="")

# Close the File
filePointer.close()

Output
KRISHNA
MADAN
MOHAN
MANOHAR
MURARI
[/code]

जैसाकि हम देख सकते हैं कि इस Program का Output भी Exactly वही है, जो पिछले Program का था। अन्‍तर केवल इतना है कि इस Program में हमने File को Read करने के लिए किसी File Reading Method का प्रयोग नहीं किया है, बल्कि हमने for…in Loop के माध्‍यम से File Object के Iterator को Use करते हुए File के Content को Line by Line Output में Display कर दिया है।

इस Chapter में हम Python द्वारा Provide किए जाने वाले विभिन्‍न Programming Constructs के बारे में केवल Basic बातें समझ रहें हैं इसलिए अन्‍य Concepts की तरह ही हम File के बारे में भी यहां पर कुछ ज्‍यादा Deep Discussion नहीं करेंगे। लेकिन Python हमें File Management से सम्‍बंधित विभिन्‍न प्रकार के Operations Perform करने के लिए Functions व Methods की पूरी एक Library Provide करता है, जिनके बारे में हम आगे आने वाले Chapter में जब भी जरूरत होगी, विस्‍तार से जानेंगे।

फिर भी एक File Object के साथ हम किन-किन Operations को Perform कर सकते हैं, उनसे सम्‍बंधित Attributes की List देखने के लिए हम dir() Function का प्रयोग कर सकते हैं और किसी Perticular Method के बारे में अधिक जानकारी प्राप्‍त करने के लिए हम help() Method का प्रयोग कर सकते हैं।

उदाहरण के‍ लिए यदि हम जानना चाहते हैं कि एक File Object के साथ कौन-कौन से Attributes Available रहते हैं, तो हम निम्‍नानुसार एक Simple सा Example Program Create कर सकते हैं और उसमें dir() Function को Use करके File Object द्वारा सभी Supported Attributes की List को Output के रूप में देख सकते हैं-

[code]
FileName: FileObjectAttributes.py
# Create a New File in Writing Mode using 'w'
filePointer = open("names.txt", "r")

# Display all Supported Attributes of the File Object
print(dir(filePointer), "\n\n")
print("--------------- Display Help for seek() Method ---------------")
help(filePointer.seek)

# Close the File
filePointer.close()

Output
['_CHUNK_SIZE', '__class__', '__del__', '__delattr__', '__dict__', '__dir__', '__doc__', '__enter__', '__eq__', '__exit__', '__format__', '__ge__', '__getattribute__', '__getstate__',
'__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__lt__', '__ne__', '__new__', '__next__()', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '_checkClosed', '_checkReadable', '_checkSeekable', '_checkWritable', '_finalizing', 'buffer', 'close', 'closed', 'detach', 'encoding', 'errors', 'fileno', 'flush', 'isatty', 'line_buffering', 'mode', 'name', 'newlines', 'read', 'readable', 'readline', 'readlines', 'seek', 'seekable', 'tell', 'truncate', 'writable', 'write', 'writelines']

------------------------------ Display Help for seek() Method ------------------------------
Help on built-in function seek:
seek(cookie, whence=0, /) method of _io.TextIOWrapper instance
    Change stream position.
    Change the stream position to the given byte offset. The offset is
    interpreted relative to the position indicated by whence.  Values
    for whence are:
    * 0 -- start of stream (the default); offset should be zero or positive
    * 1 -- current stream position; offset may be negative
    * 2 -- end of stream; offset is usually negative
    Return the new absolute position.
[/code]

Python in Hindi - BccFalna.comये Article इस वेबसाईट पर Selling हेतु उपलब्‍ध EBook Python in Hindi से लिया गया है। इसलिए यदि ये Article आपके लिए उपयोगी है, तो निश्चित रूप से ये EBook भी आपके लिए काफी उपयोगी साबित होगी।

Python in Hindi | Page: 602 | Format: PDF

BUY NOW GET DEMO REVIEWS