Ask a user to select a folder to read the files in Python


If you have ever wondered how the dialogboxes work in a Python application, then you probably end up hearing the filedialog module in Tkinter. The filedialog module contains a number of built-in functions which can be used to display various types of dialogs for dealing with files in the system.

In most cases, we use the filedialog.askopenfilename() function to ask the user to browse and open a file from the system. Based on the selection of the filetype, the script is programmed to perform write or read operation.

Once a file is opened, you can use the open(file, 'mode') function to open and perform operations in any mode. To demonstrate this, let's take an example where we will create an application that will ask the user to open a text file. Once a file is selected and opened, we can read this file using the "read" mode of operation.

Example

# Import the library
from tkinter import *
from tkinter import filedialog

# Create an instance of window
win=Tk()

# Set the geometry of the window
win.geometry("700x300")

# Create a label
Label(win, text="Click the button to open a dialog", font='Arial 16 bold').pack(pady=15)

# Function to open a file in the system
def open_file():
   filepath = filedialog.askopenfilename(title="Open a Text File", filetypes=(("text    files","*.txt"), ("all files","*.*")))
   file = open(filepath,'r')
   print(file.read())
   file.close()

# Create a button to trigger the dialog
button = Button(win, text="Open", command=open_file)
button.pack()

win.mainloop()

Output

Running the above code will display a window with a button to open a dialog.

Select and open a text file and the console will display all the content of the file.

Centralized Database Vs Blockchain

A blockchain can be both permissionless (like Bitcoin or Ethereum) or permissioned (like the different Hyperledger blockchain frameworks). A permissionless blockchain is also known as a public blockchain, because anyone can join the network. A permissioned blockchain, or private blockchain, requires pre-verification of the participating parties within the network, and these parties are usually known to each other.

Types of Blockchains
The choice between permissionless versus permissioned blockchains should be driven by the particular application at hand (or use case). Most enterprise use cases involve extensive vetting before parties agree to do business with each other. An example where a number of businesses exchange information is supply chain management. The supply chain management is an ideal use case for permissioned blockchains.

You would only want trusted parties participating in the network. Each participant that is involved in the supply chain would require permissions to execute transactions on the blockchain. These transactions would allow other companies to understand where in the supply chain a particular item is.

Updated on: 22-Dec-2021

8K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements