Python File Handling क्या है? Open, Read, Write & Append in Hindi | RBSE Class 12
Python File Handling क्या है?
File Handling का अर्थ computer में मौजूद files को Python program की सहायता से create, open, read, write और modify करना है।
जब हमें किसी data को program बंद होने के बाद भी सुरक्षित रखना होता है, तो हम उसे file में store कर सकते हैं।
Python में File Handling के लिए मुख्य रूप से open() function का उपयोग किया जाता है।
Python में File Handling क्यों जरूरी है?
- Data को permanently store करने के लिए।
- Existing file से data पढ़ने के लिए।
- File में नया data लिखने के लिए।
- Existing file में नया data जोड़ने के लिए।
- Large amount of text data को manage करने के लिए।
open() Function
Python में file खोलने के लिए open() function का उपयोग किया जाता है।
Syntax
open("filename", "mode")
उदाहरण:
file = open("student.txt", "r")
यहाँ student.txt file का नाम है और r file mode है।
Python File Modes
| Mode | उपयोग |
|---|---|
| r | File को read करने के लिए |
| w | File में write करने के लिए |
| a | File के अंत में data जोड़ने के लिए |
| x | नई file create करने के लिए |
| r+ | Read और write दोनों के लिए |
| w+ | Write और read दोनों के लिए |
| a+ | Append और read दोनों के लिए |
1. Read Mode — r
r mode का उपयोग existing file को पढ़ने के लिए किया जाता है।
file = open("student.txt", "r")
data = file.read()
print(data)
file.close()
यदि specified file मौजूद नहीं है, तो read mode में file खोलने पर error हो सकता है।
r का अर्थ Read है।
2. Write Mode — w
w mode का उपयोग file में data लिखने के लिए किया जाता है। यदि file मौजूद नहीं है तो Python इसे create कर सकता है।
file = open("student.txt", "w")
file.write("Rahul Kumar")
file.close()
Important: यदि existing file को w mode में
open किया जाता है, तो उसमें मौजूद पुराना content overwrite हो सकता है।
w का अर्थ Write है और existing content
overwrite हो सकता है।
3. Append Mode — a
a mode का उपयोग existing file के अंत में नया data जोड़ने के लिए किया जाता है।
file = open("student.txt", "a")
file.write("\nClass 12")
file.close()
Append mode में नया content सामान्यतः existing content के बाद add होता है।
a का अर्थ Append है।
4. Exclusive Creation Mode — x
x mode का उपयोग नई file create करने के लिए किया जाता है। यदि उसी नाम की file पहले से मौजूद है, तो error हो सकता है।
file = open("newfile.txt", "x")
file.write("This is a new file")
file.close()
x mode नई file create करने के लिए उपयोग किया जाता है।
File में Data Write करना
File में text लिखने के लिए write() method का उपयोग किया जाता है।
file = open("message.txt", "w")
file.write("Welcome to Python")
file.close()
एक से अधिक Lines Write करना
file = open("student.txt", "w")
file.write("Rahul\n")
file.write("Class 12\n")
file.write("Python")
file.close()
यहाँ \n नई line के लिए उपयोग किया गया है।
writelines() Method
writelines() method का उपयोग strings की sequence को file में write करने के लिए किया जा सकता है।
lines = [
"Python\n",
"Java\n",
"C++\n"
]
file = open("languages.txt", "w")
file.writelines(lines)
file.close()
writelines() अपने आप प्रत्येक item के बीच newline नहीं
जोड़ता। जरूरत होने पर strings में \n देना पड़ता है।
read() Method
read() method file का content पढ़ने के लिए उपयोग किया जाता है।
file = open("student.txt", "r")
data = file.read()
print(data)
file.close()
read(size) का उपयोग
read() में संख्या देने पर specified number of characters
तक data पढ़ा जा सकता है।
file = open("student.txt", "r")
data = file.read(10)
print(data)
file.close()
readline() Method
readline() method file से एक line पढ़ने के लिए उपयोग किया जाता है।
file = open("student.txt", "r")
line = file.readline()
print(line)
file.close()
readlines() Method
readlines() method file की lines को पढ़कर एक List के रूप में return करता है।
file = open("student.txt", "r")
lines = file.readlines()
print(lines)
file.close()
read(), readline() और readlines() में अंतर
| Method | उपयोग |
|---|---|
| read() | File का पूरा या specified amount data पढ़ता है। |
| readline() | एक line पढ़ता है। |
| readlines() | Lines को List के रूप में पढ़ता है। |
close() Method
File का काम पूरा होने के बाद उसे बंद करने के लिए close() method का उपयोग किया जाता है।
file = open("student.txt", "r")
data = file.read()
print(data)
file.close()
with Statement
Python में file handling के लिए with statement का उपयोग करना convenient और सुरक्षित तरीका है। इसके साथ block पूरा होने पर file को automatically close किया जा सकता है।
with open("student.txt", "r") as file:
data = file.read()
print(data)
इस तरीके में अलग से file.close() लिखने की जरूरत सामान्यतः
नहीं रहती।
File में Text Write करके Read करना
with open("student.txt", "w") as file:
file.write("Rahul Kumar\n")
file.write("Class 12\n")
file.write("Python")
with open("student.txt", "r") as file:
data = file.read()
print(data)
Output:
Rahul Kumar
Class 12
Python
Append करके Data Add करना
with open("student.txt", "a") as file:
file.write("\nRBSE Student")
इससे existing content के बाद नया text add किया जा सकता है।
File का पूरा Path देना
यदि file current working directory में नहीं है, तो file का path दिया जा सकता है।
file = open("data/student.txt", "r")
Windows path के साथ backslash का उपयोग करते समय raw string या उचित escaping का ध्यान रखना चाहिए।
File Exists है या नहीं Check करना
File के existence को check करने के लिए Python के os.path.exists() का उपयोग किया जा सकता है।
import os
if os.path.exists("student.txt"):
print("File exists")
else:
print("File does not exist")
File Delete करना
File को delete करने के लिए os.remove() का उपयोग किया जा सकता है।
import os
os.remove("student.txt")
File Handling का Practical Example
नीचे एक simple student record को text file में store किया गया है।
name = input("Enter your name: ")
marks = input("Enter your marks: ")
with open("student.txt", "w") as file:
file.write("Name: " + name + "\n")
file.write("Marks: " + marks)
print("Data saved successfully.")
File Handling में Common Errors
FileNotFoundError
यदि read mode में ऐसी file open की जाती है जो मौजूद नहीं है, तो FileNotFoundError हो सकता है।
file = open("abc.txt", "r")
FileExistsError
यदि x mode में ऐसी file create करने की कोशिश की जाए जो
पहले से मौजूद है, तो FileExistsError हो सकता है।
Exception Handling के साथ File Reading
try:
with open("student.txt", "r") as file:
data = file.read()
print(data)
except FileNotFoundError:
print("File not found.")
Python File Handling Important Methods
| Function / Method | उपयोग |
|---|---|
| open() | File open/create करना |
| read() | Data पढ़ना |
| readline() | एक line पढ़ना |
| readlines() | Lines को List में पढ़ना |
| write() | Data लिखना |
| writelines() | Multiple strings लिखना |
| close() | File बंद करना |
| os.path.exists() | File मौजूद है या नहीं check करना |
| os.remove() | File delete करना |
File Modes Quick Revision
| Mode | Meaning | मुख्य उपयोग |
|---|---|---|
| r | Read | File पढ़ना |
| w | Write | File लिखना / overwrite करना |
| a | Append | अंत में data जोड़ना |
| x | Exclusive Creation | नई file create करना |
| r+ | Read + Write | Read और write |
| w+ | Write + Read | Write और read |
| a+ | Append + Read | Append और read |
RBSE Class 12 Important Questions
- File Handling क्या है?
- Python में file open करने के लिए कौन-सा function उपयोग होता है?
- open() function का syntax लिखिए।
- File mode क्या होता है?
- r mode का उपयोग क्या है?
- w mode का उपयोग क्या है?
- a mode का उपयोग क्या है?
- x mode का उपयोग क्या है?
- read() method क्या करता है?
- readline() और readlines() में अंतर लिखिए।
- write() method का उपयोग बताइए।
- writelines() method क्या करता है?
- close() method का उपयोग क्या है?
- with statement का File Handling में क्या उपयोग है?
- r और w mode में क्या अंतर है?
- w और a mode में क्या अंतर है?
- FileNotFoundError कब होता है?
- FileExistsError कब हो सकता है?
- os.path.exists() का उपयोग क्या है?
- os.remove() का उपयोग क्या है?
Frequently Asked Questions (FAQs)
Python File Handling क्या है?
Python File Handling का उपयोग files को create, open, read, write, append और manage करने के लिए किया जाता है।
Python में file open करने के लिए कौन-सा function उपयोग होता है?
Python में file open करने के लिए open() function का
उपयोग किया जाता है।
r और w mode में क्या अंतर है?
r mode file को read करने के लिए है, जबकि w mode file में write करने के लिए उपयोग होता है और existing content overwrite हो सकता है।
a mode क्या करता है?
a यानी append mode existing file के अंत में नया data जोड़ने के लिए उपयोग किया जाता है।
read() और readline() में क्या अंतर है?
read() file का पूरा या specified amount data पढ़ सकता है, जबकि readline() एक line पढ़ने के लिए उपयोग किया जाता है।
with open() का क्या फायदा है?
with statement का उपयोग करने पर block समाप्त होने के बाद file को automatically close किया जा सकता है।
अब अगला Topic पढ़ें
Python File Handling की विस्तृत जानकारी के लिए Python की official documentation देखें।