Home >> Python Tutorial >> File Handling
Edit Template
Edit Template

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 में हमे देता है।

🐍
Example 1.py
Copy to clipboard
f = open("demo.txt","r")

print(f.read())
print(f.readline())
print(f.readlines())
f.close()

Writing to a file

किसी file में write करने के लिए python में write() और writelines() methods का use किया जाता है।

write() : इस method से file में एक बार में एक ही line लिखा किया जा सकता है।

writelines() : इस methods से file में multiple lines एक साथ लिखा जा सकता है।

🐍
Example 2.py
Copy to clipboard
#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()

“with” statement

अगर हम file को with statement के साथ open करते है तो हमे file को close करने के लिए close() का use करने की जरूरत नहीं होती, file automatically close हो जाती है।

🐍
Example 3.py
Copy to clipboard
with open("demo.txt","r") as f:
    data = f.read()
    print(data)

उम्मीद करते है कि आपको File Handling अच्छे समझ मे आ गया होगा । अपने learning को continue रखने के लिए next button पर click करे,

Add a comment...

  • All Posts
  • Artificial Intelligence
  • Computer Fundamentals
  • Computer Networks
  • Data Analytics
  • Data Science
  • DBMS
  • Deep Learning
  • Digital Fundamentals
  • 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
File Pointer

Table of Contents All Chapters 1. File Pointer 2. File Pointer Methods      2.1. tell() Method      2.2....

File Handling

Table of Contents All Chapters 1. File Handling 2. Types of File 3. open() and close() Methods 4. File Modes...

Python Modules

Table of Contents All Chapters 1. Modules in Python 2. Pre-defined Modules      2.1. math      2.2. random...

Python Iterators

Table of Contents All Chapters 1. Iterators      1.1. Iterable      1.2. Iterator      1.3. Iterator Protocol...

Python Polymorphism

Table of Contents All Chapters 1. Polymorphism 2. Key Concepts of Polymorphism 3. Types of Polymorphism      3.1. Polymorphism...

Python Inheritance

Table of Contents All Chapters 1. Inheritance 2. Concepts of Inheritance 3. Types of Inheritance      3.1. Single Inheritance...

Python Function

Table of Contents All Chapters 1. Python Function 2. Define Function 3. Function Arguments 4. Function Parameters 5. Local and...

Python Dictionary

Table of Contents All Chapters 1. Python Dictionary 2. Access Dictionary items 3. Change Dictionary items 4. Add items in...

Edit Template
Scroll to Top