Python Full Course In Hindi

Home About Us Contact Us Privacy Policy

Introduction TO File Handling

File Handling का मतलब है | कि किसी file को create , close , read , open करना | Python के इस फीचर के उपयोग से किसी दूसरी के साथ काम किया जा सकता है | Python मे file Handling के लिए mainly निम्नलिखित operations किए जाते है |

1. Open() किसी एक python file मे किसी अलग python file को open करने के लिए open() का उपयोग किया जाता है |

जैसे कि
🐍 Syntax
file = open("Hello.txt", "r")

2.close() किसी file का काम पूरा होने के बाद file को close करने के लिए close() function का उपयोग किया जाता है |

🐍 Syntax
file = open("Hello.txt", "r") content = file.read() print(content) file.close()

File मे write करना

3."w"(write) mode File मे कुछ Write करने के लिए किसी file को "write" mode मे open किया जाता है |

🐍 Syntax
file = open("Hello.txt", "w") file.write("Hello how are you.") file.close()

File को read करना

4."r"(read) mode केवल File को read करने के लिए file को "read" mode मे open किया जाता है |

🐍 Syntax
file = open("Hello.txt", "r") content = file.read() print(content) file.close()

Append Mode

5."a"(append) mode file मे कछ data जोड़ने के लिए "append" mode का उपयोग किया जाता है |

🐍 Syntax
file = open("Hello.txt", "r") file.write("\nHello how are you.") file.close()

Exception Handling with Files

Exception Handling python का एक बहुत अहम भाग(part) है | जो file के साथ किए जा रहे operations के दौरान हुई errors को handle करता है | ये erros हो सकते है
file नही मिली |
file crash हो गई etc.
python मे इन errors को handle करने के लिए try और execpt का उपयोग किया जाता है |

🐍 Syntax
try: # pehle kisee file ko open karne kee koshish with open("example.txt", "r") as file: content = file.read() print(content) except fileNotFoundError: print("Error : aapki file nhi mili") except PermissionError: print("Error:File ko access karne ki permission allow nhi hai") finally: print("File kaa operation poora hua")