Introduction
Reading files is one of the most common operations in Python programming. Python provides easy-to-use methods for reading entire files, reading line by line, and more.
Below are 10 examples to help you understand different ways to read files in Python with detailed explanations.
फाइल पढ़ना Python प्रोग्रामिंग में सबसे सामान्य ऑपरेशनों में से एक है। Python आसान methods प्रदान करता है पूरी फाइल पढ़ने, लाइन दर लाइन पढ़ने और अन्य तरीके से पढ़ने के लिए।
नीचे 10 उदाहरण दिए गए हैं जो Python में फाइल पढ़ने के विभिन्न तरीकों को समझाने में मदद करेंगे, विस्तार से explanations के साथ।
10 Examples of Reading Files in Python
Example 1: Read Entire File Using read() Copy file = open('sample.txt', 'r')
content = file.read()
print(content)
file.close()Output: Prints entire content of 'sample.txt'.
This opens 'sample.txt' in read mode, reads all content at once, prints it, and closes the file.
'sample.txt' को पढ़ने के लिए खोलता है, पूरी सामग्री पढ़ता है, print करता है और फाइल बंद करता है।
Example 2: Read File Line by Line Using readline() Copy file = open('sample.txt', 'r')
line1 = file.readline()
line2 = file.readline()
print(line1)
print(line2)
file.close()Output: Prints first two lines of the file separately.
Reads first line and second line individually using readline() and prints them.
readline() का उपयोग कर पहली और दूसरी लाइन पढ़ता है और उन्हें print करता है।
Example 3: Read All Lines as a List Using readlines() Copy file = open('sample.txt', 'r')
lines = file.readlines()
print(lines)
file.close()Output: Prints a list of all lines with newline characters.
Reads all lines and returns them as a list of strings, then prints the list.
सभी लाइनें पढ़कर उन्हें एक list के रूप में प्राप्त करता है और print करता है।
Example 4: Iterate Over File Object Line by Line Copy file = open('sample.txt', 'r')
for line in file:
print(line.strip())
file.close()Output: Prints each line after removing leading/trailing spaces.
Iterates over the file object directly, printing each line with whitespace stripped.
फाइल ऑब्जेक्ट पर सीधे loop लगाकर प्रत्येक लाइन को whitespace हटाकर print करता है।
Example 5: Using with Statement to Read Entire File Copy with open('sample.txt', 'r') as file:
content = file.read()
print(content)Output: Prints entire file content safely without explicit close.
'with' statement handles closing file automatically after reading all content.
'with' स्टेटमेंट फाइल को automatic बंद कर देता है पढ़ने के बाद।
Example 6: Using with Statement to Read Line by Line Copy with open('sample.txt', 'r') as file:
for line in file:
print(line.strip())Output: Prints each line without extra spaces safely.
Reads file line by line inside 'with' block, stripping spaces and printing.
'with' ब्लॉक के अंदर लाइन दर लाइन पढ़ता है और print करता है।
Example 7: Reading Large Files in Chunks Copy with open('sample.txt', 'r') as file:
while True:
chunk = file.read(50) # reads 50 characters
if not chunk:
break
print(chunk)Output: Reads and prints file content in chunks of 50 characters.
Useful for large files, reads fixed-size chunks to avoid loading entire file at once.
बड़ी फाइलों के लिए chunks में पढ़ता है जिससे memory बचती है।
Example 8: Reading Binary File Copy with open('image.png', 'rb') as file:
content = file.read(100)
print(content)Output: Prints first 100 bytes of binary file (image.png).
Reads first 100 bytes of a binary file in binary mode 'rb'.
binary फाइल के पहले 100 bytes पढ़ता है।
Example 9: Reading File with Specified Encoding Copy with open('utf8file.txt', 'r', encoding='utf-8') as file:
content = file.read()
print(content)Output: Reads UTF-8 encoded text file properly.
Specifies encoding while opening to correctly read files with special characters.
खास encoding के साथ फाइल पढ़ने के लिए encoding पैरामीटर का उपयोग।
Example 10: Using try-except for File Reading Errors Copy try:
with open('missing.txt', 'r') as file:
content = file.read()
print(content)
except FileNotFoundError:
print('File not found!')Output: Prints error message if file does not exist.
Handles file not found error gracefully using try-except.
यदि फाइल न मिले तो error मैसेज दिखाने के लिए try-except।