'Searching through a list of text files for the correct user and their information in Python
I'm very new to Python and I am making a simple program, which starts off with a menu. One of the options, once selected, will ask the user to enter a username. Once a username is entered, the program will search through a list of text files in that directory (Each user will have their own text file, potentially named after them so that when the username is entered, it finds that user) until it finds the right user, and displays their information in the program.
So the output would be like this Enter the username: ProPlayer27 (Searching through a list of text files in the same directory for this username) Result found - Proplayer27 + all of the information from that specific text file
I am really unsure of how to do this, I have tried various methods but with little to no success.
I would really appreciate any help I can get!
I understand that in order for all of the text files to be read in the first place, os.listdir needs to be used, so I am using that as a place to start. I have tried to divide it into 3 steps:
Take the input from the user
Search the text files for the user's input
Once the text file has been found, display it onto the program.
I have little to nothing to show, but I know that multiple text files in a directory can be found like this:
for file in os.listdir("D:\Test"):
if file.endswith(".txt"):
print(os.path.join("Test", file))
Solution 1:[1]
The following will do what you want using tkinter.
import tkinter as tk
import os
from pathlib import Path
def find_info():
data = user_input.get() #gets the user input in the entry field
entry.delete(0, tk.END) #delete the input after button pressed
nothing = '' # used ensure nothing happens if the entry is blank
folder = "test data" # name of the folder where the text files exist
connected = os.path.join(folder, data + ".txt") # joins the input for search
record = Path(connected) # used for readability to check its existance
lines_read = [] # empty list to store the data you will read
if data != nothing: # make sure the entry was not left blank
if record.exists(): # checks if the record infact exists
with open(connected, "r") as x: # open and read the text file
for every_line in x.read().split("{}"): # list/string handling
if every_line not in lines_read: # check against empty list
lines_read.append(every_line) # writes the content to the list
result = "".join(lines_read) # format and join
check_label["text"] = result # display resutls on the check_label
root = tk.Tk()
root.geometry("400x200")
root.resizable(False, False)
root.title("Sample Program")
user_input = tk.StringVar()
entry = tk.Entry(root, text=user_input, width=20)
entry.pack()
button = tk.Button(root, text="Find Entry", command=lambda: find_info())
button.pack()
check_label = tk.Label(root, font="Times 12")
check_label.pack()
root.mainloop()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
