File Handling
जब computer पर कोई data लिखा जाता है | तब वो पहले अपने RAM (Random Access Memory) पर store होता है और जब computer को turn off हो जाता है तब वो सारा data lose हो जाता है | और जब उस data को save किया जाता है | तब वो computer के Secondary Storage Device (Hard drive) पर permanently store किया जाता है |
दूसरे शब्द में : File Handling वह प्रक्रिया है जिसमें Python किसी टेक्स्ट या अन्य प्रकार की फ़ाइल के साथ कार्य करता है।
Uses :
- Data Store करना
- Reports बनाना
- Log files रखना
- Student Record save करना
- Configuration files पढ़ना
Python में file को handle करने के लिए अनेक प्रकार के operation किए जाते हैं |
- Opening file
- Closing file
- Reading file
- Writing file
- Renaming file
- Deleting file
Opening File
File के data को जब manipulate करना हो तो पहले file को open किया जाता है | python में file को open करने के लिए python के in-built ‘open()’ method का use किया जाता है |
Syntax :
fileObj = open(fileName, mode)
Example :
file = open('textfile.txt') #default mode is 'r'
#or
file = open('textfile.txt', 'r')
Modes for opening a file
| Mode | Full Name | Purpose | यदि File मौजूद न हो | यदि File पहले से मौजूद हो |
|---|---|---|---|---|
r | Read | केवल File पढ़ने के लिए | ❌ Error देता है | File को Read करता है |
w | Write | File में नया Data लिखने के लिए | ✅ नई File बनाता है | पुराना Data मिटाकर नया लिखता है |
a | Append | File के अंत में नया Data जोड़ने के लिए | ✅ नई File बनाता है | पुराना Data सुरक्षित रहता है, नया Data अंत में जुड़ता है |
x | Exclusive Create | केवल नई File बनाने के लिए | ✅ नई File बनाता है | ❌ Error देता है (यदि File पहले से हो) |
r+ | Read + Write | File को पढ़ना और लिखना | ❌ Error देता है | Read और Write दोनों कर सकते हैं |
w+ | Write + Read | File को पढ़ना और लिखना | ✅ नई File बनाता है | पुराना Data हटाकर नया लिखता है, फिर Read भी कर सकते हैं |
a+ | Append + Read | Data जोड़ना और पढ़ना | ✅ नई File बनाता है | Data अंत में जोड़ता है, पुराना Data नहीं हटता |
rb | Read Binary | Binary File (Image, PDF आदि) पढ़ने के लिए | ❌ Error देता है | Binary Data Read करता है |
wb | Write Binary | Binary File लिखने के लिए | ✅ नई File बनाता है | पुराना Binary Data हटाकर नया लिखता है |
ab | Append Binary | Binary File के अंत में Data जोड़ने के लिए | ✅ नई File बनाता है | Binary Data अंत में जोड़ता है |
rb+ | Read + Write Binary | Binary File पढ़ना और लिखना | ❌ Error देता है | Read और Write दोनों कर सकते हैं |
wb+ | Write + Read Binary | Binary File लिखना और पढ़ना | ✅ नई File बनाता है | पुराना Binary Data हटाकर नया लिखता है |
ab+ | Append + Read Binary | Binary Data जोड़ना और पढ़ना | ✅ नई File बनाता है | Data अंत में जोड़ता है और Read भी कर सकते हैं |
Closing file
जब file को open() method से open किया जाता है तब file पर कुछ operation करके close भी किया जाता है | File को close करने के लिए close() method का इस्तेमाल किया जाता है close() method को जब file object की मदद से call किया जाता है | तब file पर write हुए hidden data को flush किया जाता है | जहां पर file को close किया जाता है उसके बाद बिना file open किये उस file पर कोई भी operation नहीं किया जा सकता है |
Syntax :
fileObj.close()
Source Code :
file = open("textfile.txt",'a') #Opening File
file.close() #Closing File
Reading File
File का data read करने के लिए read() method का use किया जाता है |
Syntax :
fileObj.read(size)
Source Code :
file = open("textfile.txt",'r')
print(file.read())
file.close()
Writing File
File पर data को write करने के लिए write() method का use किया जाता है |
Syntax :
fileObj.write(str)
Source code :
file = open("textfile.txt",'w+')
file.write("Hello")
file.close()
Renaming File
File को rename करने के लिए ‘os’ module को import करके उसके rename() method का इस्तेमाल किया जाता है |
Syntax :
os.rename(fileName, newFileName)
Source Code :
import os
os.rename("textfile.txt", "myfile.txt")
#rename textfile.txt to myfile.txt
Removing File
File को remove करने के लिए ‘os’ module को import करके उसके remove() method का इस्तेमाल किया जाता है |
Syntax :
os.remove(fileName)
Source Code :
import os
os.remove("myfile.txt")
#removed file 'myfile.txt'
File Method
| Method | Purpose | Return Type | Example |
|---|---|---|---|
| open() | File को Open करता है | File Object | file = open("data.txt", "r") |
| close() | File को बंद करता है | None | file.close() |
| read() | पूरी File या दिए गए Characters पढ़ता है | String | file.read() |
| read(size) | निश्चित Characters पढ़ता है | String | file.read(10) |
| readline() | एक Line पढ़ता है | String | file.readline() |
| readlines() | सभी Lines को List के रूप में पढ़ता है | List | file.readlines() |
| write() | File में Data लिखता है | Integer (Characters की संख्या) | file.write("Hello") |
| writelines() | List की कई Lines एक साथ लिखता है | None | file.writelines(["A\n","B\n"]) |
| seek() | File Pointer को नई Position पर ले जाता है | Integer | file.seek(0) |
| tell() | वर्तमान File Pointer की Position बताता है | Integer | file.tell() |
| flush() | Buffer का Data तुरंत File में Save करता है | None | file.flush() |
| truncate() | File का Size कम (Cut) करता है | Integer | file.truncate(20) |
| readable() | जाँचता है कि File Read हो सकती है या नहीं | Boolean | file.readable() |
| writable() | जाँचता है कि File Write हो सकती है या नहीं | Boolean | file.writable() |
| seekable() | जाँचता है कि Pointer Move हो सकता है या नहीं | Boolean | file.seekable() |
- Python Full Course Roadmap 2026Python Introduction↓Python Installation & Setup↓Python Basics↓Variables & Data Types↓Operators↓Conditional Statements↓Loops↓Functions↓Data Structures↓File Handling↓Exception Handling↓Object Oriented Programming (OOP)↓Modules & Libraries↓Advanced Python↓Database Connectivity↓Web Development↓Data Science & AI↓Automation & Projects↓Python Developer
- What is Python in Hindi (Basic to advanced)Introduction and History Introduction for Python Python History Python Features Python Applications Applications Uses 1. Artificial Intelligence (AI) and Machine Learning Python का उपयोग AI models, Machine Learning algorithms, Chatbots, Face Recognition, Voice Assistant और Prediction systems बनाने के लिए किया जाता है। 2. Data Science and Data Analysis Python से बड़े datasets को analyze… Read more: What is Python in Hindi (Basic to advanced)
- Python Keywords in Hindi (easy to understand )Python Keywords में ऐसे special word होते हैं जिनका Python भाषा में पहले से एक Predefined meaning होता है। Python Interpreter इन words को पहचानता है और इनके अनुसार कार्य करता है। Example : No. Keyword हिंदी में अर्थ Uses (उपयोग) Syntax 1 False गलत False keywords का use Boolean False value के लिए किया… Read more: Python Keywords in Hindi (easy to understand )
- Python Variables in Hindi (Easy to understand)what is Variable Assigning Value to variable Source Code : Output : 5 Hello [2, 5, 9] Changing Variable’s Values Source Code : Output : 5Hello[4, 5, 8] Assigning Single Value to Multiple Variables Source Code : Output : HelloHelloHelloHello Assigning Value to Variable according to order Source Code : Output : 1H[1, 2] Variable… Read more: Python Variables in Hindi (Easy to understand)
- Python Data Types in Hindi (easy to understand)Python में Data Type यह बताता है कि किसी variable में किस प्रकार का data store है। Python में data type को manually declare नहीं करना पड़ता, Python interpreter value देखकर data type पहचान लेता है। Example : Number Python में Number Data Types का उपयोग संख्याओं (Numbers) को store और process करने के लिए… Read more: Python Data Types in Hindi (easy to understand)
