Python फ़ाइल मौजूद है या नहीं, इसकी जाँच करें (उदाहरणों सहित)
⚡ स्मार्ट सारांश
Python यह आपको os.path मॉड्यूल और pathlib का उपयोग करके यह जांचने की सुविधा देता है कि कोई फ़ाइल या निर्देशिका मौजूद है या नहीं। यहां आप os.path.exists(), os.path.isfile(), os.path.isdir() और pathlib.Path.exists() के बारे में जानेंगे, जिनमें से प्रत्येक को स्पष्ट कोड उदाहरण के साथ समझाया गया है।

Python मौजूद()
Python मौजूद() विधि का उपयोग यह जाँचने के लिए किया जाता है कि कोई विशिष्ट फ़ाइल या निर्देशिका मौजूद है या नहीं। इसका उपयोग यह जाँचने के लिए भी किया जाता है कि कोई पथ किसी खुले फ़ाइल डिस्क्रिप्टर को संदर्भित करता है या नहीं। यदि फ़ाइल मौजूद है तो यह बूलियन मान true लौटाता है और अन्यथा false लौटाता है। इसका उपयोग os मॉड्यूल और os.path उप मॉड्यूल के साथ os.path.exists(path) के रूप में किया जाता है।
इस में Python फ़ाइल मौजूद है ट्यूटोरियल में, हम सीखेंगे कि कैसे निर्धारित करें कि कोई फ़ाइल (या निर्देशिका) मौजूद है या नहीं Python. यह जाँचने के लिए कि क्या फ़ाइल मौजूद है Python, हम अंतर्निहित लाइब्रेरी का उपयोग करते हैं Python जाँचें कि क्या फ़ाइल मौजूद है फ़ंक्शन.
किसी फ़ाइल या खाते को सत्यापित करने के विभिन्न तरीके हैं. Python नीचे सूचीबद्ध फ़ंक्शन का उपयोग करके जाँच करें कि निर्देशिका मौजूद है या नहीं।
कैसे जांचें कि कोई फ़ाइल मौजूद है या नहीं Python os.path.exists() का उपयोग करके
Path.exists का उपयोग करके आप जल्दी से जाँच सकते हैं कि कोई फ़ाइल या निर्देशिका मौजूद है या नहीं। इसके लिए यहाँ चरण दिए गए हैं Python जाँचें फ़ाइल मौजूद है या नहीं:
चरण 1) os.path मॉड्यूल आयात करें
कोड चलाने से पहले, यह महत्वपूर्ण है कि आप os.path मॉड्यूल आयात करें।
import os.path from os import path
चरण 2) path.exists() फ़ंक्शन का उपयोग करें
अब, path.exists() फ़ंक्शन का उपयोग करें Python जाँच करें कि क्या कोई फ़ाइल मौजूद है.
path.exists("guru99.txt")
चरण 3) नीचे दिया गया कोड चलाएँ
यहाँ पूरा कोड है
import os.path from os import path def main(): print ("File exists:"+str(path.exists('guru99.txt'))) print ("File exists:" + str(path.exists('career.guru99.txt'))) print ("directory exists:" + str(path.exists('myDirectory'))) if __name__== "__main__": main()
हमारे मामले में केवल फ़ाइल guru99.txt कार्यशील निर्देशिका में बनाई गई है
आउटपुट:
File exists: True File exists: False directory exists: False
Python इसफाइल ()
RSI Python इसफाइल () विधि का उपयोग यह पता लगाने के लिए किया जाता है कि दिया गया पथ एक मौजूदा नियमित फ़ाइल है या नहीं। यदि विशिष्ट पथ एक मौजूदा फ़ाइल है तो यह बूलियन मान true लौटाता है अन्यथा यह false लौटाता है। इसका उपयोग सिंटैक्स द्वारा किया जा सकता है: os.path.isfile(path)।
os.path.isfile()
हम isfile कमांड का उपयोग यह जांचने के लिए कर सकते हैं कि दिया गया इनपुट फ़ाइल है या नहीं।
import os.path from os import path def main(): print ("Is it File?" + str(path.isfile('guru99.txt'))) print ("Is it File?" + str(path.isfile('myDirectory'))) if __name__== "__main__": main()
आउटपुट:
Is it File? True Is it File? False
ओएस.पथ.आईएसडीआईआर()
यदि हम यह पुष्टि करना चाहते हैं कि दिया गया पथ किसी निर्देशिका की ओर इंगित करता है, तो हम os.path.dir() फ़ंक्शन का उपयोग कर सकते हैं
import os.path from os import path def main(): print ("Is it Directory?" + str(path.isdir('guru99.txt'))) print ("Is it Directory?" + str(path.isdir('myDirectory'))) if __name__== "__main__": main()
आउटपुट:
Is it Directory? False Is it Directory? True
pathlibPath.exists() के लिए Python 3.4
Python 3.4 और उससे ऊपर के संस्करणों में फ़ाइल सिस्टम पथ के साथ काम करने के लिए pathlib मॉड्यूल है। यह ऑब्जेक्ट-ओरिएंटेड दृष्टिकोण का उपयोग करता है Python जाँचें कि फ़ोल्डर मौजूद है या नहीं.
import pathlib file = pathlib.Path("guru99.txt") if file.exists (): print ("File exist") else: print ("File not exist")
आउटपुट:
File exist
पूर्ण Code
यहाँ पूरा कोड है
import os from os import path def main(): # Print the name of the OS print(os.name) #Check for item existence and type print("Item exists:" + str(path.exists("guru99.txt"))) print("Item is a file: " + str(path.isfile("guru99.txt"))) print("Item is a directory: " + str(path.isdir("guru99.txt"))) if __name__ == "__main__": main()
आउटपुट:
Item exists: True Item is a file: True Item is a directory: False
