कभी-कभी, एप्लिकेशन बनाते समय, हमें बाहरी प्रोग्राम और एप्लिकेशन के साथ सहभागिता करने की आवश्यकता होती है। सिस्टम के अनुप्रयोगों और कार्यक्रमों के साथ बातचीत करने के लिए, हमें os . का उपयोग करना होगा पायथन में मॉड्यूल।
इस लेख में, हम देखेंगे कि हम पायथन में ओएस मॉड्यूल का उपयोग करके बाहरी कार्यक्रमों और ओपनफाइल्स के साथ कैसे इंटरैक्ट कर सकते हैं।
सबसे पहले, हम एक फ़ंक्शन को परिभाषित करेंगे जो filedialog . का उपयोग करके चुनी गई फ़ाइल को खोलेगा पायथन में पुस्तकालय। फिर, हम पथ को प्रिंट करेंगे और os . का उपयोग करके फ़ाइल को खोलेंगे मॉड्यूल।
उदाहरण
# Import the required Libraries
from tkinter import *
from tkinter import filedialog
import os
#Create an instance of tkinter frame
win= Tk()
#Set the geometry for the window or frame
win.geometry("600x400")
#Define a function to open the application
def app():
file= filedialog.askopenfilename()
text.config(text= file)
#Open the program
os.system('"%s"' %file)
#Create a button
Button(win, text='Click to Open a Program',font=('Poppins bold', 10),
command=app).pack(pady=20)
#Create a Label after button event
text= Label(win, text= "", font= ('Poppins bold', 10))
text.pack(pady=20)
#Keep running the window or frame
win.mainloop() आउटपुट
उपरोक्त कोड को चलाने से निम्न विंडो आउटपुट के रूप में उत्पन्न होगी -

अब, बटन पर क्लिक करें और यह "मेरे दस्तावेज़" फ़ोल्डर को खोलेगा जहाँ से आप एक प्रोग्राम खोल सकते हैं।