Introduction TO File Handling
FIle Handling Python language का एक बहुत महात्वपूर्ण concept है , जिसको समझना जरूरी है | किसी file को खोलना , उस File मे write करना , File को close करना आदि काम करना , Python मे file Handling कहलाता है | शब्दों में एक Python File से किसी अलग .txt file को connect करके (open करके) उस file मे काम करना या उस file को handle करना file Handling कहलाता है |
Files क्या होती है ?
Files किसी भी data को computer में store करने की unit होती है | For Example जिस तरह Kitchen सामान रखने के लिए boxes होते है , उसी तरह से computer मे data को store करने के लिए Files होती है | जब भी हम हमारा कोई Project , content या कुछ भी बनाते है | तो हमे Future मे उसमे update , read , write etc. काम करने की need होती है | जिसके लिए हम उस project को एक file के रूप मे save कर देते है | Files एक Storage यूनिट होती है | जिसमे data store किया जाता है | Python मे mainly Files दो प्रकार की होती है | Text और Binary Files
Text Files क्या है ?
Text Files वे FIles होती है , जो human readable होती है | यानि हम Text Files मे Stored data को देख
सकते है , Read कर सकते है | Text Files मे characters , numbers , punctuation आदि data store किया जाता है |
Text Files मे data को store करने के लिए encoding का उपयोग किया जाता है | encoding हमारे द्वारा लिखे
गए text को Binary मे convert करता है जैसे :-
a को (01000001) मे
ऐसा इसलिए किया जाता है | क्योकि computer हमारी language नही समझता है | वो 0 और 1 को समझता है |
Python मे किसी File को कैसे open करे |
Python मे Text File को कैसे खोले इस step से File Handling की शुरूआत होती है | Python मे किसी अलग File को खोलेने के लिए open() का उपयोग किया जाता है | open() एक built in function होता है | जिसका उपयोग किसी file को open करने के लिए किया जाता है | open function का Syntax नीचे दिया गया है |
1open("filename", "mode")
Syntax Explanations
open() किसी और File को open करने के लिए open function का
उपयोग किया जाता है | फिर
open("filename", "mode") जिस file को open करना है | Double quotes मे उस file का
नाम दिया जाता है | फिर
open("filename", "mode") , देकर Double quotes मे mode दिया जाता है कि file को किस
mode मे open किया जाना है |
किसी file को open करने के बहुत mode होते है जैसे write mode , read mode , append mode etc. हर mode को नीचे समझाया गया है |
NOTE : इन modes का use try.py File मे किया गया है , example.txt file के साथ काम करने के लिए |
1. read mode
किसी File के अंदर के data को केवल read करने के लिए File को read mode मे open किया जाता है | इस mode मे File को open करने से File मे stored data safe रहता है | यानि stored data मे कोई changes नही किए जा सकते है | example.txt को try.py मे run करने या open करने के लिए try.py मे code लिखा गया है |
1file = open("example.txt", "r") 2data = file.read() 3print(data)
This is the example of read mode.
Syntax Explanations
file = open("example.txt", "r") file नाम का एक variable लिया गया है |
जिसमे example.txt को read mode मे open करने के लिए लिखा गया है | फिर
data = file.read() read function example.txt मे लिखे गए सारे content को read करता
है और सारे content को data variable मे store करता है | फिर
print(data) अब example.txt का सारा content data variable मे store है और उस content
को display करने के लिए print function का उपयोग किया गया है |
2. write mode
write mode file मे stored सारे content को हटा के open करता है और write mode से file मे content write किया जा सकता है | write mode को "w" से defin किया जाता है जैसे :-
1file = open("example.txt", "w") 2file.write("Hello User")
Hello User
Syntax Explanations
file = open("example.txt", "w") example.txt को write mode मे open किया गया है |
और example.txt मे stored सारे content को file variable store किया गया है फिर
file.write("Hello User") file variable मे stored सारे content को clear करके
example.txt मे Hello User लिखे |
3. Append mode
write mode file मे stored data को delete करके file को open करता है | जबकि append mode file से कुछ भी data delete किए बिना file को open करता है और file मे data भी add करने देता है |append mode को "a" से define किया जाता है जैसे :-
1file = open("example.txt", "a") 2file.write("This is new line")
Hello User
This is new line
Syntax Explanations
file = open("example.txt", "a") example.txt को append mode मे open किया गया है |
और example.txt के सारे data को file variable मे store किया गया गया है | फिर
file.write("This is new line") file variable मे stored content मे ये line add करो |
2. File को close करना |
किसी file का काम को पूरा होने के बाद file को close करने के लिए close() का उपयोग किया जाता है | नीचे एक close() का उदाहण दिया गया है |
1file = open("example.txt", "r") 2file.close()
Hello User
This is new line
Syntax Explanations
file = open("example.txt", "r") example.txt को read mode मे open किया गया है | फिर
file.close() file को read करने के बाद file को close किया गया है |
3. writelines()
writelines के use से किसी file मे एक साथ कई lines लिख सकते है | और file मे कई lines एक साथ लिखने के लिए file को write mode मे open किया जाता है | जैसे :-
1file = open("example.txt", "w") 2lines = ["Python is\n", "good\n"] 3file.writelines(lines)
Hello User
This is new line
Syntax Explanations
file = open("example.txt", "w") example.txt को write mode मे open किया गया है | और
example.txt मे stored content को file variable मे store किया गया है | फिर
lines = ["Python is\n", "good\n"] new line के लिए \n का Use किया जाता है और string को
quotes मे लिखा जाता है | फिर
file.writelines(lines) file variable मे stored data मे lines variable मे लिखे गए data
को writelines () के Use add करो |
4. File मौजूद है |
आप जिस file को handle करना चाहते है , या open करना चाहते है वो file folder मे है या नही ये पता करने के लिए एक os Module का उपयोग किया जाता है |
1import os 2 3if os.path.exists("example.txt"): 4 print("File Found") 5else: 6 print("File Not Found")
Syntax explanations
import os os नाम के Module को import किया गया है |
if os.path.exists("example.txt"): condition देने के लिए if का Use किया है |
condition दि गई है कि यदि example.txt folder मे है , तो नीचे दिए गए print function मे लिखे गए message को display करो | फिर
else : यदि if condition ture नही होती है तो else : statement काम करेगी |
5. File को delete करना
अगर आप example.txt को delete करना चाहते है | तो file को delete करने के लिए os Module का उपयोग किया जाता है |
1import os 2 3os.remove("example.txt")
Syntax Explanations
import os os Module को import किया गया है | फिर
os.remove("example.txt") लिखा गया है कि example.txt को delete करो |
इन सभी methods के उपयोग से किसी text file को handle किया जा सकता है |
Text Files के लाभ
1. Text File human readable होती है | जिसके कारण file मे data को store करना , read करना etc. काम
करना सरल है |
2. Text Files को use करना सरल है | और text files को mobiles मे भी create किया जा सकता है |
3. Text Files mobiles computers मे Binary files के मुकाबले space कम लेती है |
4. Text Files को एक mobile से दूसरे mobile या एक computer से दूसरे computer मे share करना आसान है |
5. Text Files को vs code जैसे code editor मे open किया जा सकता है |
अब Binary files क्या होती है? Binary files को क्या हम control कर सकते है आदि का नीचे वर्णन किया गया है |
Binary Files क्या है ?
Binary files वो files होती है जो human readable नही होती है | यानि computer readable होती है | Binary files मे data 0 और 1 के रूप मे store होता है और 0 और 1 के रूप मे data को computer ही समझ सकता है | पर हम Binary files को handle कर सकते है | images audio की files Binary format की files होती है | Binary files को "rb" read Binary मे पढ़ा जाता है | और "wb" write Binary मे लिखा जाता है |
Binary File की विशेषताएँ
1. Machine Readable Binary Files Machine readable होने के कारण इनको केवल Machine ही पढ़ सकती है |
ये Files
human readable नही होती है |
2. Format Binary Files मे data 0 और 1 मे store होता है |
3. editor not support Binary files को vs code जैसे editors support नही करते है |
4. space Binary Files मे data 0 और 1 मे store होने के कारण Binary Files अधिक
space लेती है |
Diffrence Between Text Files And Binary Files
| Base | Text File | Binary FIle |
|---|---|---|
| 1. Meaning | Text file मे text data store किया जाता है | | Binary file मे 0 और 1 मे data store होता है | |
| 2. size | Text file का size छोटा होता है | | Binary file का size बडा होता है | |
| 3. Readable | Text Files Human readable होती है | | Binary Files Machine readable होती है | |
| 4. Modes | Text File के कुछ "w" "r" "a" modes है | | Binary File के कुछ "rb", "wb" modes है | |
| 5. Examples | Text File के कुछ .txt, .html, .py है | | Binary File के कुछ .jpg, .mp3, .png है | |
अध्याय समाप्त — आगे बढ़ें
आपने इस chapter मे python के बहुत ही महत्वपूर्ण concepts समझे है जो आगे की प्रोग्रामिंग सीखने मे बहुत Helpful हैं। आगे के chapter मे आप Exception Handling के बारे मे सिखेगे आगे के chapter की और बढ़ने के लिए नीचे दिए गए बटन पर क्लिक करे |
Next Chapter
Seekho Coding