I made a random password generator for a project and it works fine, but when it generates passwords, it always adds " { " to a password or two. Why does it add this and how do I fix it?
Code:
import os
import random
from Tkinter import *
from tkMessageBox import *
class Application(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.grid()
self._passwordLength = StringVar()
self._passwordLength.set("")
self._passwordAmount = StringVar() # create a String control variable
self._passwordAmount.set("")
self._fileName = StringVar()
self._fileName.set("")
top = self.winfo_toplevel() # find top-level window
top.title("Random Password Generator")
self._createWidgets()
def _createWidgets(self):
siteLabel = Label(self, text="Password Lengths Wanted:", anchor = E, width = 20)
siteLabel.grid(row = 0, column = 0)
siteLabel = Label(self, text="Password Amount Wanted:", anchor = E, width = 20)
siteLabel.grid(row = 1, column = 0)
siteLabel = Label(self, text="File Name:", anchor = E, width = 20)
siteLabel.grid(row = 2, column = 0)
lengthEntry = Entry(self, takefocus = 1,
textvariable = self._passwordLength)
lengthEntry.grid(row = 0, column = 1, sticky=E+W)
amountEntry = Entry(self, takefocus = 1,
textvariable = self._passwordAmount)
amountEntry.grid(row = 1, column = 1, sticky=E+W)
fileEntry = Entry(self, takefocus = 1,
textvariable = self._fileName)
fileEntry.grid(row = 2, column = 1, sticky=E+W)
self._button = Button(self, text = "Generate", command = self._getData, width = 20)
self._button.grid(row = 3, column = 0)
self._button = Button(self, text = "Quit", command = self._quitProgram, width = 20)
self._button.grid(row = 3, column = 1)
def _getData(self):
try:
passwordLength = int(self._passwordLength.get())
passwordAmount = int(self._passwordAmount.get())
except:
showwarning("Error","Please check you input.")
self._generatePasswords(passwordLength, passwordAmount)
def _generatePasswords(self, passwordLength, passwordAmount):
fileName = str(self._fileName.get())
if ".txt" not in fileName:
fileName += ".txt"
if os.path.isfile(fileName):
if askyesno("Verify", "This file already exists, would you like to overwrite it?"):
fileToWrite = open(fileName, "w")
else:
fileToWrite = open(fileName, "w")
try:
randomize(passwordLength, passwordAmount, fileToWrite)
if True:
showwarning("Random Password Generator", "Passwords Created! Check your folder!")
except:
showwarning("Error"," Please check your input.")
def _quitProgram(self):
if askyesno('Verify', 'Are you sure you would like to quit the program?'):
root.destroy()
# end class Application
def main():
Application().mainloop()
def randomize(length, amount, fileToWrite):
for num in xrange(amount):
newPassword = ""
for count in xrange(length):
randomInt = random.randint(0,26)
newPassword += chr(randomInt+97)
count += 1
fileToWrite.write(newPassword+"\n")
fileToWrite.close()
root = Tk()
main()
Comment