Posted By Naveen Singh on 03 Apr 2023
python-program
किसी भी 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 कर सकते हैं।
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 करने के लिए हम built in open() function का प्रयोग करते हैं। जिसमे हम दो parameters pass करते है ( filename, mode), filename का मतलब है कि वो file जिसे आप open करना चाहते है और mode का मतलब है कि किस mode में (read, write, append)
Example-:
f = open("new.txt", "r")
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
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 में हम किसी भी 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 करने के लिए हमे कुछ महत्वपूर्ण 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 करने के लिए हमे OS module को import करना पड़ता है जिसके साथ हमे os.remove() function का भी प्रयोग करना पढ़ता है।
Example-:
import os
os.remove("newfile.txt")
I Hope आपको Python में file handling क्या होते है? open() function python में कैसे work करता है, और file में modes के बारे में आपको जानकारी मिली होगी। यदि आपको कोई doubt है तो comment section में comment करके पूछ सकते हैं।
payal yadav
thank you that's why very helpful....
03/04/2023