
Table of Contents All Chapters 1. File Pointer 2. File Pointer Methods 2.1. tell() Method 2.2....
Python code का use करके जब हम files को create, read, write और update करते है तो इसे file handling in python कहते है। जब हमे data को permanent store करना होता है तो हम file handling का use करते है।
1.Text File : ऐसे files जिसमे texts, numbers होते है उन्हे text file कहते है, इन files को हम read कर सकते है। जैसे : .txt, .csv, .log,
2. Binary File : images, videos, software executables जैसे files को binary file कहा जाता है, ये human-readable नहीं होती है। जैसे : .jpg, .mp4, .exe,
open() : python में file को open करने के लिए open() method का use करते है।
close() : python में किसी open file को close करने के लिए close() method का use किया जाता है। क्योंकि जब file पर काम जो जाए तो उसे close कर देना चाहिए इससे ये ensure हो जाता है की data properly save हो गए है, और file close कर देने से memory free भी हो जाती है।
जब हम python code से file को open करते है तो हमे एक mode देना होता है की हम file को किसलिए open कर रहे है read करने के लिए, write करने के लिए या किसी और काम के लिए file को open कर रहे है।
“r” : इसे read mode कहते है अगर हम इस mode में file को open करते है तो हम file के data को सिर्फ read (पढ़) ही कर सकते है।
“w” : इसे write mode कहते है अगर हम इस mode में file को open करते है तो उस file का सारा data overwrite हो जाएगा मतलब उस file में हम कोई data डालेंगे तो old data ये जगह अब new वाला data दिखाई देगा। but हमने जो file write mode में open किया है अगर वो file exist ही नहीं करता है तो write mode उस name का new file create कर देगा।
“a” : इसे append mode कहते है हम इस mode में file को open तब करते है जन हमे उस file के end में कोई data डालना होता है।
“x” : इसे exclusive mode कहते है जब हम इस mode में file को open करते है तो अगर इस नाम का file पहले से मौजूद है तो हमे error show होगा, और अगर ऐसा कोई भी file पहले से नहीं है तो exclusive mode इस नाम का new file create कर देगा।
“b” : इसे binary mode कहते है जब है binary file (मतलब image, videos वाली files) को open करना होता है तो हम binary mode का use करते है।
“w+” : इसे write and read mode कहते है, इस mode में file को open करने पर हम read और write दोनों कर सकते है, अगर हमने जो file open किया है वो इसे नहीं मिलता तो ये new file create कर देता है। इस mode में जब हम file में write करते है तो file का data overwrite हो जाता है।
“r+” : इसे read and write mode कहते है, इस mode में file को open करने पर हम read और write दोनों कर सकते है, अगर हमने जो file open किया है वो इसे नहीं मिलता तो ये error देता है। इस mode में जब हम file में write करते है तो file का data overwrite हो जाता है।
Python में file को read करने के लिए read(), readline() और readlines() methods का use किया जाता है।
read() : ये methods किसी file का सारा content एक साथ read करता है।
readlines() : ये methods file में से सिर्फ एक line को ही read करता है।
readlines() : ये methods किसी file के सारे lines को एक list के form में हमे देता है।
f = open("demo.txt","r")
print(f.read())
print(f.readline())
print(f.readlines())
f.close()
किसी file में write करने के लिए python में write() और writelines() methods का use किया जाता है।
write() : इस method से file में एक बार में एक ही line लिखा किया जा सकता है।
writelines() : इस methods से file में multiple lines एक साथ लिखा जा सकता है।
#write()
f = open("demo.txt","w")
f.write("Python File Handling Example\n")
f.write("Second line")
#writelines()
lines = ["Line 1\n", "Line 2\n", "Line 3\n"]
f = open("demo.txt","w")
f.writelines(lines)
f.close()
अगर हम file को with statement के साथ open करते है तो हमे file को close करने के लिए close() का use करने की जरूरत नहीं होती, file automatically close हो जाती है।
with open("demo.txt","r") as f:
data = f.read()
print(data)
उम्मीद करते है कि आपको File Handling अच्छे समझ मे आ गया होगा । अपने learning को continue रखने के लिए next button पर click करे,
Table of Contents All Chapters 1. File Pointer 2. File Pointer Methods 2.1. tell() Method 2.2....
Table of Contents All Chapters 1. File Handling 2. Types of File 3. open() and close() Methods 4. File Modes...
Table of Contents All Chapters 1. Modules in Python 2. Pre-defined Modules 2.1. math 2.2. random...
Table of Contents All Chapters 1. Iterators 1.1. Iterable 1.2. Iterator 1.3. Iterator Protocol...
Table of Contents All Chapters 1. Polymorphism 2. Key Concepts of Polymorphism 3. Types of Polymorphism 3.1. Polymorphism...
Table of Contents All Chapters 1. Inheritance 2. Concepts of Inheritance 3. Types of Inheritance 3.1. Single Inheritance...
Table of Contents All Chapters 1. Classes and Objects 2. Class in Python 3. Class Structure 3.1. Attributes...
Table of Contents All Chapters 1. Python Function 2. Define Function 3. Function Arguments 4. Function Parameters 5. Local and...
Table of Contents All Chapters 1. Python Dictionary 2. Access Dictionary items 3. Change Dictionary items 4. Add items in...