1

I finished up my first GUI project that allows the user to open any PDF file by wither typing a product key, or manually find it using a tree view. I have all the files in the same folder as the application, the problem is that when I used pyinstaller to make it an .exe it wont pull up. and anytime I move it nothing pulls up because the code is linked to that specific location. my question is how do you write it to where the filepath is linked to a specific folder, and even if said folder moves, it still accesses those files?

def Product_Look_UP():
    if inPro.get()== "24LPPS":
        Label(root, text= f"Searching for {inPro.get()}..." , fg = "Orange").grid(row = 3 , column = 0)
        import webbrowser as wb
        wb.open_new(r'C:\Users\user\Desktop\Product Finder\Data\Vender\vender\Ladder Pulls\24 in\Polished Stainless\LADDER PULL.pdf')

Essentially how do make "C:\Users\user\Desktop" be where ever the "Product Finder" folder is, whether it be on my computer, the server, or another persons computer?

Matiiss
  • 5,970
  • 2
  • 12
  • 29

1 Answers1

2

You can call the function os.path.abspath and pass it the file name of your main python script and it will return the path where the file is. You can do something like this:

import os
import webbrowser as wb

CURRENT_DIR = os.path.dirname(os.path.abspath(__file__))
wb.open_new(f"{CURRENT_DIR}/data/test.pdf")
SAL
  • 547
  • 2
  • 8
  • 25