Blog

File Handling in python in hindi - Bharosewale

Posted By Naveen Singh on 03 Apr 2023

python-program
python-me-file-handling-kya-hote-hai-in-hindi

Python में file handling क्या है?( What is file handling in python?)

 

File handling in python in Hindi

 

किसी भी web application में file handling एक important part होता है। Python हमे कुछ महत्वपूर्ण function प्रदान करता है जिसकी सहायता से हम किसी भी file को create, update, read,

delete कर सकते हैं। Python में एक बहुत ही important key function होता है जिसे हम open() function कहते हैं इसका  प्रयोग हम files के साथ कार्य करने के लिए करते हैं। file handling में

open () function दो parameters लेता है, जिसमे पहला होता है file name और दूसरा होता है mode इन्ही दो parameters पर ही open() function work करता है। Python में हम file को चार

different methods(mode) की सहायता से open कर सकते हैं।

  • Read("r")- इसका प्रयोग हम file को read करने के लिए करते हैं, अगर file मौजूद नहीं है तो error show हो जाएगा
  • Append("a") - इसका प्रयोग हम file को append करने के लिए करते हैं, अगर file मौजूद नहीं है तो यह file create कर देता है।
  • Write ("w")- इसका प्रयोग हम file में कुछ write करने के लिए करते हैं, अगर file मौजूद नहीं है तो यह उसको create कर देता है।
  • Create ("x") - इसका प्रयोग हम file को create करने के लिए करते हैं, अगर file मौजूद होती है तो यह error show कर देता है।
  • Text("t")- इसका प्रयोग हम file को text mode में open करने के लिए करते हैं|
  • Binary("b")- इसका प्रयोग हम file को binary mode में open करने के लिए करते हैं। e.g- images

Syntax-:

f = open(filename, mode)

Example-:

f = open("new.txt","r")

ऊपर दिए गए example में open() function में हम दो parameters को पास करते हैं जिसमे पहला file name होता है और दूसरा किस mode में हमे उस file को open करना है जिसमे हम उस file को read, write, append, text आदि रूप मे open कर सकते हैं।


 

Python में file को open कैसे करें?( How to open file in python?)

Python में file को open करने के लिए हम built in open() function का प्रयोग करते हैं। जिसमे हम दो parameters pass करते है ( filename, mode), filename का मतलब है कि वो file जिसे आप open करना चाहते है और mode का मतलब है कि किस mode में (read, write, append)

Example-:

f = open("new.txt", "r")


 

Python में read() function क्या होता है?( What are read () function in python?)

 

Python में हम किसी भी file को open करने के लिए built in open() function का उपयोग करते हैं जो कि एक file object return करता है।

open() function जो file object return करता है, जिसमें read()  method की सहायता से हम किसी भी file के content को read कर सकते है।

Example-:

f = open("file.txt", "r")

print(f.read())

Output-:

This is a file object open with read mode

अगर file किसी दूसरी location पर हो तो फिर हमें उस file के path को देना पड़ता हैं।

Example-:

f = open("C:\\files\new.txt", "r")

print(f.read())

Output-:

This is a python file located on different location

Read Only Parts of the File -:  अगर आपको file में से कुछ characters को ही read करना हैं तो उसके लिए आपको read() function में  paramter मै value को pass करना पड़ेगा।

Example-:

f =open("file.txt","r")

print(f.read(4))

Output-:

This

Read Lines-: इस method की सहायता से आप file में से एक line return कर सकते हैं।

Example-:

f = open("file.txt", "r")

print(f.readline())

अगर आप Read Line method को दो बार लिखते है तो इससे आप file में से दो line return कर देता है।

Example-:

f = open("newfile.txt", "r")

print(f.readline())

Output-:

This is a read() in file handling in python


 

Python में close() function का क्या प्रयोग होता है?( What is the use of close function in python?)

 

close() function की सहायता से हम किसी भी file को close कर सकते हैं। इसका प्रयोग हम तब करते हैं जब हम किसी file को use कर चुके हो और हम उस file को close करना चाहते है।

Example-:

f = open("newfile.txt", "r")

print(f.readline())

f.close()

Output-:

This is a python file


 

Python में write function क्या होता है?( What is write function in python?)

 

Python में हम किसी भी existing file में दो तरह से write कर सकते हैं। जिसको हमे open() function में parameter के रूप में pass करना पढ़ता है।

Append("a")-: इसमें हम जो भी content write करते है वो content file के end में जाके append हो जाता है, इस सहायता से हम किसी भी file के अंत में append कर सकते हैं ।

Example-:

f = open("newfile2.txt", "a")

f.write("this is a append method !")

f.close()

# open and read the file after the appending

f = open("newfile2.txt", "r")

print(f.read())

Output-:

This is a python file this is a append method !

Write("w")-:  इसकी सहायता से हम file में हम content को overwrite कर सकते हैं, और इसका प्रयोग हम basically file में content को write करने के लिए करते हैं।

Example-:

f = open("newfile3.txt", "w")

f.write("This is a write method in python!")

f.close()

# open and read the file after the overwriting

f = open("newfile3.txt", "r")

print(f.read())

Output-:

This is a write method in python!


 

Python में हम new file कैसे create करे?( How to create new file in python?)

 

Python में new file create करने के लिए हमे कुछ महत्वपूर्ण parameters का प्रयोग करना पढ़ता है।

create("x")-:  इसकी सहायता से हम new file create कर सकते हैं, अगर bydefault file पहले से create होती है तो ये error देता है।

Append ("a")-:  इसकी सहायता से भी हम new file create कर सकते हैं, अगर कोई specified file create नही है।

Write("w")-:  इसकी सहायता से भी हम new file create कर सकते हैं, अगर कोई specified file create नही है।

 

इन सभी parameters को हमे open() function के अंदर pass करना होता है।

Example-:

f = open("newfile.txt", "x")

f = open("newfile.txt", "a")

f = open("newfile.txt", "w")


 

Python में file को delete कैसे करे?( How to delete file in python?)

 

Python में किसी file को delete करने के लिए हमे OS module को import करना पड़ता है जिसके साथ हमे os.remove() function का भी प्रयोग करना पढ़ता है।

Example-:

import os

os.remove("newfile.txt")


 

Conclusion
 

I Hope आपको Python में file handling क्या होते है? open() function python में कैसे work करता है, और  file में modes के बारे में आपको जानकारी मिली होगी। यदि आपको कोई doubt है तो comment section में comment करके पूछ सकते हैं।

Comments


payal yadav

thank you that's why very helpful....

03/04/2023