0

I am a novice with Python. Currently, I am trying to create an application that uses a GUI button to generate a random 16 character password and then the user can press a save button to create/append the passwords to a text file. The process works with no issues inside PyCharm (generates passwords, creates a text file if it doesn't exist and appends the passwords with no issues.) However, once I create the application using PyInstaller, the text file creation no longer functions. I assume this has to do with locating file paths, but I am not skilled enough to figure the best solution. Essentially, I want the program to save the files in a "Resources/Items" folder that is in the application folder that the program can reference each time the program opens. I am currently running PyCharm on my Macbook, but I can switch to my PC if necessary. Attaching my code below:

"""Password Generator by TTVG"""

import string
import secrets
from tkinter import *
import os
from os.path import *

# Path Variables
dir = os.path.dirname(__file__)
filename = os.path.join(dir, "Password Generator", "Resources", "Images", "icon.ico")
filePath = os.path.join(dir, "Password Generator", "Resources", "Items", "passwords.txt")

# GUI Variables
root = Tk()
root.geometry("500x250")
root.title('Password Generator v1')
root.iconbitmap(filename)
varPassword = ""


def generatePassword():
    symbols = '!@#$%^&*()'
    alphabet = string.ascii_letters + string.digits + symbols
    while True:
        password = ''.join(secrets.choice(alphabet) for i in range(16))
        if (any(c.islower() for c in password)
                and any(c.isupper() for c in password)
                and sum(c.isdigit() for c in password) >= 3):
            break
    appLabel.config(text=password, font=("Helvetica", 40))
    global varPassword
    varPassword = password
    return varPassword


def savePassword(password):
    if exists(filePath):
        with open('passwords.txt', 'a+') as f:
            f.write(password + "\n")
        appLabel2.config(text="File Loaded Successfully")
        appLabel3.config(text="Password Saved Successfully")
        f.close()
    else:
        with open('passwords.txt', 'w+') as f:
            f.write(password + "\n")
        appLabel2.config(text="File Created Successfully")
        appLabel3.config(text="Password Saved Successfully")
        f.close()


# Label Widget
appLabel = Label(root, text="")
appLabel.config(font=("Helvetica", 40))
appLabel.pack(padx=5, pady=5)

# Button Widget
appButton = Button(text='Generate Password', command=generatePassword)
appButton.pack(padx=5, pady=5)

# Button Widget
appButton2 = Button(text='Save Password', command=lambda: savePassword(varPassword))
appButton2.pack(padx=5, pady=5)

# Label Widget
appLabel2 = Label(root, text="")
appLabel2.pack(padx=5, pady=5)

# Label Widget
appLabel3 = Label(root, text="")
appLabel3.pack(padx=5, pady=5)


if __name__ == '__main__':
    root.mainloop()

TTVG
  • 1
  • 1
  • 2
    Does this answer your question? [Determining application path in a Python EXE generated by pyInstaller](https://stackoverflow.com/questions/404744/determining-application-path-in-a-python-exe-generated-by-pyinstaller) – Ahmed AEK Oct 30 '22 at 07:13

0 Answers0