Reading from a File (File से Data पढ़ना) :-
Python में किसी File में पहले से मौजूद Data को Program के माध्यम से पढ़ने की प्रक्रिया को Reading from a File कहते हैं।
Python में File से Data पढ़ने के लिए मुख्य रूप से read(), readline() और readlines() Methods का उपयोग किया जाता है।
English: Reading from a File is the process of retrieving or accessing the data stored in a file using a Python program.
In Python, the read(), readline(), and readlines() methods are mainly used to read data from a file.
File से Data पढ़ने के लिए आवश्यक Steps -
File से Data Read करने के लिए सामान्यतः निम्न Steps अपनाए जाते हैं:
Open the File: open() Function की सहायता से File को Open करना।
Read the Data: read(), readline() या readlines() Method का उपयोग करके Data पढ़ना।
Close the File: काम पूरा होने के बाद close() Method से File को Close करना।
Syntax:
file = open("filename.txt", "r")
data = file.read()
print(data)
file.close()
read() Method -
read() Method का उपयोग File के Data को पढ़ने के लिए किया जाता है। यदि इसमें कोई Argument नहीं दिया जाता है, तो यह File Pointer की Current Position से लेकर File के अंत तक पूरा Data Read करता है।
English: The read() method is used to read data from a file. If no argument is provided, it reads all the remaining content from the current file position to the end of the file.
Syntax:
file.read()
या
file.read(size)
यहाँ size बताता है कि कितने Characters या Bytes Read करने हैं। Text Mode में size Characters की संख्या बताता है।
Example 1: पूरी File पढ़ना
मान लीजिए demo.txt File में निम्न Data है:
Hello Python
Welcome to File Handling
Python Program:
file = open("demo.txt", "r", encoding="utf-8")
data = file.read()
print(data)
file.close()
Output:
Hello Python
Welcome to File Handling
Explanation:
open() File को Read Mode में Open करता है।
read() पूरी File का Data Read करता है।
print() Read किए गए Data को Display करता है।
close() File को Close करता है।
Example 2: निश्चित Characters पढ़ना
file = open("demo.txt", "r", encoding="utf-8")
data = file.read(5)
print(data)
file.close()
Output:
Hello
Explanation: read(5) File Pointer की Current Position से 5 Characters Read करता है।
readline() Method :-
readline() Method का उपयोग File से एक बार में एक Line पढ़ने के लिए किया जाता है। इसे बार-बार Call करने पर अगली Line Read होती है।
English: The readline() method is used to read one line at a time from a file.
Syntax:
file.readline() Example:
मान लीजिए demo.txt File में निम्न Data है:
Hello Python
Welcome to File Handling
Python is Easy
Python Program:
file = open("demo.txt", "r", encoding="utf-8")
line1 = file.readline()
line2 = file.readline()
print(line1, end="")
print(line2, end="")
file.close()
Output:
Hello Python
Welcome to File Handling
Explanation:
पहली बार readline() Call करने पर पहली Line Read होती है।
दूसरी बार readline() Call करने पर दूसरी Line Read होती है।
end="" अतिरिक्त New Line को Print होने से रोकता है।
Important: यदि File में और कोई Line नहीं बची है, तो readline() Empty String ('') Return करता है।
readlines() Method :-
readlines() Method का उपयोग File की सभी Remaining Lines को एक साथ पढ़कर List के रूप में प्राप्त करने के लिए किया जाता है।
English: The readlines() method reads all remaining lines from a file and returns them as a list of strings.
Syntax:
file.readlines() Example:
मान लीजिए demo.txt File में निम्न Data है:
Hello Python
Welcome to File Handling
Python is Easy
Python Program:
file = open("demo.txt", "r", encoding="utf-8")
data = file.readlines()
print(data)
file.close()
Output:
['Hello Python\n', 'Welcome to File Handling\n', 'Python is Easy']
Explanation:
readlines() File की सभी Remaining Lines Read करता है।
यह Lines को List में Return करता है।
प्रत्येक Line में सामान्यतः उसका Newline Character (\n) भी शामिल होता है, यदि वह File में मौजूद हो।