Reading from a File (File से Data पढ़ना) :-
Python में किसी File में मौजूद पूरे Data को एक साथ पढ़ने की प्रक्रिया को Reading Complete File कहते हैं। इसके लिए File को Read Mode (r) में Open किया जाता है और read() Method का उपयोग किया जाता है।
English: Reading a Complete File is the process of retrieving all the data stored in a file at once using Python.
Syntax:
file = open("filename.txt", "r", encoding="utf-8")
data = file.read()
print(data)
file.close()
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.read()
print(data)
file.close()
Output:
Hello Python
Welcome to File Handling
Python is Easy
Explanation:
open() Function File को Read Mode में Open करता है।
file.read() पूरी File का Data Read करता है।
print(data) Read किए गए Data को Display करता है।
file.close() File को Close करता है।
Important Points:
पूरी File Read करने के लिए read() Method का उपयोग किया जाता है।
Read Mode (r) में File पहले से मौजूद होनी चाहिए।
Text Mode में read() String (str) Return करता है।
File में मौजूद Newline Characters भी Read किए जाते हैं।