- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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.
- Related Articles
- How to read multiple text files from a folder in Python?(Tkinter)
- Python - Read all CSV files in a folder in Pandas?
- How to read all files in a folder to a single file using Java?
- Python - How to Merge all excel files in a folder
- How to copy files from one folder to another using Python?
- How to copy certain files from one folder to another using Python?
- How to write files to asset folder in android?
- How to read text files using LINECACHE in Python
- How to obtain a list of all files in a public folder in Laravel?
- How to Copy files or Folder without overwriting existing files?
- Python Program to Read and printing all files from a zip file
- How to write files to the assets folder in android using Kotlin?
- How to get list of all files/folders from a folder in Java?
- How to upload multiple files and store them in a folder with PHP?
- How to upload files in Laravel directly into the public folder?
