Sometimes, while creating an application, we need to interact with external programs and applications. In order to interact with the system's applications and programs, we have to use os Module in python.
In this article, we will see how we can interact with external programs and open files using the OS module in Python.
First, we will define a function that will open the chosen file using the filedialog library in Python. Then, we will print the path and open the file using the os module.
# 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()
Running the above code will produce the following window as the output −
Now, click the button and it will open the “My Documents” folder from where you can open a program.