Table of Contents
Python Tutorial
Table of Contents
Python Tutorial
File Handling in Hindi
What is File Handling in Python
Python code का use करके जब हम files को create, read, write और update करते है तो इसे file handling in python कहते है। जब हमे data को permanent store करना होता है तो हम file handling का use करते है।
Types of File
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,
How to open and close a file in Python
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 भी हो जाती है।
File Modes in Python
जब हम 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 हो जाता है।
Reading a file
Python में file को read करने के लिए read(), readline() और readlines() methods का use किया जाता है।
read() : ये methods किसी file का सारा content एक साथ read करता है।
readlines() : ये methods file में से सिर्फ एक line को ही read करता है।
readlines() : ये methods किसी file के सारे lines को एक list के form में हमे देता है।
Writing to a file
किसी file में write करने के लिए python में write() और writelines() methods का use किया जाता है।
write() : इस method से file में एक बार में एक ही line लिखा किया जा सकता है।
writelines() : इस methods से file में multiple lines एक साथ लिखा जा सकता है।
“with” statement
अगर हम file को with statement के साथ open करते है तो हमे file को close करने के लिए close() का use करने की जरूरत नहीं होती, file automatically close हो जाती है।
उम्मीद करते है कि आपको File Handling अच्छे समझ मे आ गया होगा । अपने learning को continue रखने के लिए next button पर click करे,
- All Posts
- Artificial Intelligence
- Computer Fundamentals
- Computer Networks
- Data Analytics
- Data Science
- DBMS
- Deep Learning
- Digital Electronics
- DSA with Python
- Excel
- Exercise
- Git & Github
- Machine Learning
- Matplotlib
- Natural Language Processing
- NumPy
- Operating System
- Pandas-s
- Power BI
- Python Tutorial
- Scikit-learn
- Seaborn
- SQL & MySQL

Table of Contents All Chapters 1. Personal Area Network 2. Local Area Network 3. Campus Area Network 4. Metropolitan Area...

Table of Contents All Chapters 1. Data Communication 2. Types of Data Communication 2.1. Simplex 2.2....

Table of Contents All Chapters 1. Special Logic Gate 2. XOR Gate 2.1. XOR Gate Truth Table 3....

Table of Contents All Chapters 1. Universal Logic Gates 2. NOR Gate 2.1. NOR Gate Truth Table 3....

Table of Contents All Chapters 1. Logic Gate 2. AND Gate 2.1. AND Gate Truth Table 3. OR...

Table of Contents All Chapters 1. Simplification 2. Simplification using Boolean Algebra Theorems 3. Simplification using K-map Digital Electronics...

Table of Contents All Chapters 1. Identity Laws 2. Null Laws 3. Domination Laws 4. Idempotent Laws 5. Complement Laws...