I have a small tool I made for work, basically opens some websites and copies stuff to my clipboard so I can quickly answer questions that get asked a lot. I wanted to add a new feature to the bottom of my tool that can lookup the vendor of a MAC using the OUI. I have a file called oui.txt and 2 python files all in the same directory. I started by first building my mac lookup script in its own file. It looks like this….
mac = input("Enter MAC: ").upper().replace(':', '')[:6]
ouiList = []
filehandle = open("oui.txt", 'r')
dataList = filehandle.read()
for vendor in dataList.split('n'):
ouiList.append(vendor.strip())
for i in ouiList:
if mac in i:
print(i)
Pretty straight forward, it works no problem. However, when I move it over to my work tool, I consistently get an error saying it cannot find oui.txt even though both my python files and oui.txt are in the same folder. I cannot understand why one file can find it and the other cannot. I’ve tried a few different methods that I have seen suggest on here, including using the Path module to set the path, and using the OS module to set the directory, which is the current iteration of my code. I thought maybe it had to do with my code being inside an while True statement with If blocks inside of it, so I moved it outside of everything as a function, then had my if statement call the function, but the same thing happens.
Here is the current code with all the extra work stuff cut out and here is a random MAC I’ve been using to test with 90:DE:80:21:36:7E
import os
import PySimpleGUI as sg
import webbrowser as wb
from tkinter import Tk
from AppOpener import open
def get_vendor():
mac = values["-MACINPUT-"].upper().replace(':', '')[:6]
ouiList = []
here = os.path.dirname(os.path.abspath(__file__))
filename = os.path.join(here, 'oui.txt')
filehandle = open(filename, 'r')
dataList = filehandle.read()
for vendor in dataList.split('n'):
ouiList.append(vendor.strip())
for i in ouiList:
if mac in i:
window["-MACINPUT-"].update(i)
default_column = [
[sg.Input(default_text='', key="-MACINPUT-")],
[sg.OK("LOOKUP MAC", key="-LOOKUPMAC-")],
]
template_list_column = [
[sg.Column(default_column)]
]
layout = [
[sg.Column(template_list_column)]
]
window = sg.Window("Quick Links", layout, no_titlebar=True, grab_anywhere=True, resizable=True, size=(200, 500), right_click_menu=sg.MENU_RIGHT_CLICK_EDITME_VER_EXIT)
while True:
event, values = window.read()
if event == "Exit" or event == sg.WIN_CLOSED:
break
if event == "-LOOKUPMAC-":
get_vendor()
window.close()
I’ve tried finding the file with Path and Os modules. I’ve tried moving the code out of the while loop into its own function. I also slowly removed code bit by bit, but it never finds the file until the only thing left is function.
Christopher Temple is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.