|
प्रश्न 1. Python में File Open करने के लिए कौन-सा Function है? |
|
प्रश्न 4. File का पूरा Data पढ़ने के लिए कौन-सा Function है? |
|
प्रश्न 7. File Automatically Close करने के लिए क्या उपयोग करें? |
किसी भी Programming Language में Data को केवल Memory में रखना पर्याप्त नहीं होता। कई बार हमें Data को भविष्य में उपयोग करने के लिए File में Store करना पड़ता है।
Python में Files के साथ काम करने के लिए File Handling का उपयोग किया जाता है।
File Handling की सहायता से हम—
- File Create कर सकते हैं।
- File Open कर सकते हैं।
- File में Data लिख सकते हैं।
- File से Data पढ़ सकते हैं।
- File में नया Data जोड़ सकते हैं।
- File को Close कर सकते हैं।
- Text और Binary Files के साथ काम कर सकते हैं।
File Computer में Data को स्थायी रूप से Store करने का एक माध्यम है।
उदाहरण—
student.txt
marks.txt
data.csv
photo.jpg
document.pdf
Files मुख्य रूप से दो प्रकार की हो सकती हैं—
- Text File
- Binary File
Text File में सामान्यतः Text आधारित Data Store किया जाता है।
उदाहरण—
notes.txt
student.txt
data.csv
Binary Files में Data Binary Format में Store होता है।
उदाहरण—
photo.jpg
video.mp4
document.pdf
program.exe
Python में File Open करने के लिए open() Function का उपयोग किया जाता है।
open(filename, mode)
उदाहरण—
file = open("student.txt", "r")
Python में File Open करते समय Mode बताना महत्वपूर्ण है।
|
Mode |
अर्थ |
|
r |
Read |
|
w |
Write |
|
a |
Append |
|
x |
Create |
|
b |
Binary |
|
t |
Text |
|
r+ |
Read + Write |
|
w+ |
Write + Read |
|
a+ |
Append + Read |
r Mode का उपयोग File से Data पढ़ने के लिए किया जाता है।
file = open("student.txt", "r")
data = file.read()
print(data)
file.close()
यदि File मौजूद नहीं है, तो FileNotFoundError हो सकता है।
w Mode File में Data लिखने के लिए उपयोग होता है।
file = open("student.txt", "w")
file.write("Rahul")
file.close()
यदि File मौजूद नहीं है तो यह उसे Create कर सकता है।
महत्वपूर्ण: यदि File पहले से मौजूद है, तो w Mode पुराना Content Overwrite कर सकता है।
a Mode का उपयोग File के अंत में नया Data जोड़ने के लिए किया जाता है।
file = open("student.txt", "a")
file.write("\nAmit")
file.close()
पुराना Data सामान्यतः सुरक्षित रहता है और नया Data अंत में जुड़ता है।
x Mode नई File Create करने के लिए उपयोग होता है।
file = open("newfile.txt", "x")
file.write("Hello Python")
file.close()
यदि File पहले से मौजूद है, तो Error हो सकता है।
|
Mode |
कार्य |
Existing File |
|
r |
Read |
जरूरी है |
|
w |
Write |
Create/Overwrite |
|
a |
Append |
Create/Append |
read() File का Content पढ़ने के लिए उपयोग होता है।
file = open("student.txt", "r")
data = file.read()
print(data)
file.close()
यदि केवल कुछ Characters पढ़ने हों—
file = open("student.txt", "r")
data = file.read(10)
print(data)
file.close()
यह अधिकतम 10 Characters पढ़ेगा।
readline() एक समय में एक Line पढ़ता है।
file = open("student.txt", "r")
print(file.readline())
file.close()
file = open("student.txt", "r")
print(file.readline())
print(file.readline())
file.close()
readlines() File की Lines को एक List के रूप में Return करता है।
file = open("student.txt", "r")
lines = file.readlines()
print(lines)
file.close()
उदाहरण Output:
['Rahul\n', 'Amit\n', 'Neha\n']
read(), readline() और readlines() में अंतर
|
Function |
कार्य |
|
read() |
पूरा Content पढ़ता है |
|
readline() |
एक Line पढ़ता है |
|
readlines() |
सभी Lines को List में देता है |
write() File में String लिखने के लिए उपयोग होता है।
file = open("message.txt", "w")
file.write("Welcome to Python")
file.close()
file = open("student.txt", "w")
file.write("Rahul\n")
file.write("Amit\n")
file.write("Neha\n")
file.close()
writelines() Multiple Strings को File में लिखने के लिए उपयोग किया जा सकता है।
file = open("student.txt", "w")
names = ["Rahul\n", "Amit\n", "Neha\n"]
file.writelines(names)
file.close()
writelines() अपने आप हर String के बीच Newline नहीं जोड़ता। आवश्यकता हो तो \n स्वयं देना होता है।
File का काम पूरा होने के बाद उसे Close करना चाहिए।
file = open("test.txt", "r")
data = file.read()
print(data)
file.close()
Python में File Handling का बेहतर और सुरक्षित तरीका with open() है।
with open("student.txt", "r") as file:
data = file.read()
print(data)
इसका फायदा यह है कि Block समाप्त होने के बाद File को Automatically Close किया जाता है।
with open("student.txt", "w") as file:
file.write("Rahul\n")
file.write("Amit\n")
file.write("Neha\n")
with open("student.txt", "r") as file:
data = file.read()
print(data)
with open("student.txt", "a") as file:
file.write("Pooja\n")
tell() Current File Position बताता है।
with open("student.txt", "r") as file:
print(file.tell())
seek() File Pointer की Position बदलने के लिए उपयोग होता है।
with open("student.txt", "r") as file:
print(file.read(5))
file.seek(0)
print(file.read(5))
यह Pointer को फिर से Beginning पर ले जाएगा।
import os
if os.path.exists("student.txt"):
print("File exists")
else:
print("File does not exist")
import os
if os.path.exists("student.txt"):
os.remove("student.txt")
Encoding के साथ File Open करना
Hindi या अन्य Unicode Text के लिए Encoding स्पष्ट रूप से देना उपयोगी है।
with open("notes.txt", "w", encoding="utf-8") as file:
file.write("नमस्ते Python")
Read करते समय भी—
with open("notes.txt", "r", encoding="utf-8") as file:
print(file.read())
Binary File के लिए b Mode का उपयोग किया जाता है।
उदाहरण:
with open("photo.jpg", "rb") as file:
data = file.read()
यहाँ—
r → Read
b → Binary
rb → Read Binary
with open("data.bin", "wb") as file:
file.write(b"Hello")
with open("source.txt", "rb") as source:
data = source.read()
with open("copy.txt", "wb") as destination:
destination.write(data)
Practical Program 1 – Student Data Save करना
name = input("Enter Name: ")
course = input("Enter Course: ")
with open("student.txt", "w", encoding="utf-8") as file:
file.write("Name: " + name + "\n")
file.write("Course: " + course + "\n")
print("Data Saved Successfully")
Practical Program 2 – File से Data पढ़ना
with open("student.txt", "r", encoding="utf-8") as file:
data = file.read()
print(data)
Practical Program 3 – Student Add करना
name = input("Enter Student Name: ")
with open("students.txt", "a", encoding="utf-8") as file:
file.write(name + "\n")
print("Student Added")
Practical Program 4 – File में Lines Count करना
with open("students.txt", "r", encoding="utf-8") as file:
lines = file.readlines()
print("Total Lines:", len(lines))
Practical Program 5 – File में Words Count करना
with open("student.txt", "r", encoding="utf-8") as file:
data = file.read()
words = data.split()
print("Total Words:", len(words))
Practical Program 6 – File में Characters Count करना
with open("student.txt", "r", encoding="utf-8") as file:
data = file.read()
print("Total Characters:", len(data))
Practical Program 7 – File की प्रत्येक Line Print करना
with open("students.txt", "r", encoding="utf-8") as file:
for line in file:
print(line.strip())
Practical Program 8 – File में Search करना
search = input("Enter name to search: ")
with open("students.txt", "r", encoding="utf-8") as file:
data = file.read()
if search in data:
print("Name Found")
else:
print("Name Not Found")
Practical Program 9 – File में Number लिखना
with open("numbers.txt", "w") as file:
for i in range(1, 11):
file.write(str(i) + "\n")
Practical Program 10 – Numbers पढ़कर Total निकालना
यदि File में प्रत्येक Line पर एक Number है—
total = 0
with open("numbers.txt", "r") as file:
for line in file:
total += int(line.strip())
print("Total:", total)
File Modes की Complete Revision
|
Mode |
अर्थ |
|
r |
Read |
|
w |
Write / Overwrite |
|
a |
Append |
|
x |
Create New File |
|
b |
Binary |
|
t |
Text |
|
r+ |
Read + Write |
|
w+ |
Write + Read |
|
a+ |
Append + Read |
|
rb |
Read Binary |
|
wb |
Write Binary |
|
ab |
Append Binary |
मान लीजिए File में पहले से है—
Rahul
Amit
यदि:
open("student.txt", "w")
का उपयोग किया जाएगा तो पुराना Content Overwrite हो सकता है।
लेकिन:
open("student.txt", "a")
का उपयोग करने पर नया Data अंत में जोड़ने के लिए File Open होगी।
File Handling में सामान्य Errors
जब r Mode में ऐसी File Open की जाए जो मौजूद नहीं है।
with open("abc.txt", "r") as file:
print(file.read())
x Mode से ऐसी File Create करने पर जो पहले से मौजूद हो।
with open("abc.txt", "x") as file:
file.write("Hello")
जब Program के पास File को Read/Write करने की आवश्यक Permission नहीं होती।
परीक्षा की दृष्टि से महत्वपूर्ण तथ्य
- Python में File Open करने के लिए open() Function का उपयोग होता है।
- r → Read
- w → Write
- a → Append
- x → Create
- b → Binary
- t → Text
- read() → पूरा Content पढ़ सकता है।
- readline() → एक Line पढ़ता है।
- readlines() → Lines की List देता है।
- write() → String लिखता है।
- writelines() → Multiple Strings लिखता है।
- tell() → Current File Position बताता है।
- seek() → File Pointer की Position बदलता है।
- with open() → File को सही तरीके से Manage करने का Recommended तरीका है।
- w Mode Existing Content को Overwrite कर सकता है।
- a Mode Existing Content के अंत में Data जोड़ता है।
Frequently Asked Questions (FAQs)
प्रश्न 1. Python में File Open करने के लिए कौन-सा Function है?
open()
प्रश्न 2. File Read करने के लिए कौन-सा Mode है?
r
प्रश्न 3. File में नया Data जोड़ने के लिए कौन-सा Mode है?
a यानी Append Mode।
प्रश्न 4. File का पूरा Data पढ़ने के लिए कौन-सा Function है?
read()
प्रश्न 5. एक Line पढ़ने के लिए?
readline()
प्रश्न 6. File में Data लिखने के लिए?
write()
प्रश्न 7. File Automatically Close करने के लिए क्या उपयोग करें?
with open(...)
प्रश्न 8. Binary File Read करने के लिए कौन-सा Mode है?
rb
- open() → File Open
- r → Read
- w → Write
- a → Append
- x → Create
- read() → पूरा Data
- readline() → एक Line
- readlines() → Lines की List
- write() → Data Write
- writelines() → Multiple Strings Write
- tell() → Current Position
- seek() → Position Change
- with open() → Automatic File Management
- rb → Binary Read
- wb → Binary Write
अगला अध्याय —Python Exception Handling
इसमें Error और Exception, try, except, else, finally, raise, Multiple Exceptions, Custom Exception और Practical Programs विस्तार से होंगे।