Creating a list with input from askopenfile() from tkinter

Viewed 101

I am having a problem creating a list with askopenfile() with a password file. I have it working with usernames but since rockyou.txt and password files have special characters I had to convert it to UTF-8 and the same method I am using for usernames is producing a list character by character rather then word by word.

user name:

def open_file():
    file = askopenfile(mode ='r', filetypes =[('Text Files', '*.txt')])
    content = []
    userlist = []
    if file is not None:
        content = file.readlines()
        for line in content:
            userlist.append(line.strip('\n'))
        print(userlist)

username output:

['3d', 'a', 'a1', 'aa', 'aaberg', 'aachen', 'aalborg', 'aalesund', 'aalii', 'aalst', 'aalto', 'aam', 'aara', 'aarau']

(just a portion because its super long but the ideas there)

password:

def open_file2():
    file2 = askopenfile(mode ='rb', filetypes =[('Text Files', '*.txt')])
    content2 = []
    passlist = []
    if file2 is not None:
        content2 = file2.read().decode('utf-8')
        for line in passes:
            passlist.append(line.strip('\n'))
        print(passlist)

password output:

['!', '!', '!', '4', '5', '4', '5', '\r', '\n', '!', '"', '¹', '1', '2', '3', '\r', '\n', '!', '"', '¹', ';', '1', '2', '3', '4', '\r', '\n']

(also just a portion)

But yeah, the issue is the password list is being created character by character instead of word to word. Is there a way to make it word to word in the method I am currently using? Also is there a better way to open the file that is similar to the first way?

If anyone is alarmed to answer, this is for my final project(cyber security). I am now just trying to convert it to a GUI to spice things up a little.

1 Answers

I think your main issue is

for line in passes:

It should be for line in content2.splitlines(): ... I think ... I also am skeptical about the need to convert to utf8 ...but meh

dont decode your string to utf8 ... just use the bytes

def open_file2():
    file2 = askopenfile(mode ='rb', filetypes =[('Text Files', '*.txt')])
    content2 = []
    passlist = []
    if file2 is not None:
        content2 = file2.read()
        for line in content2.splitlines():
            passlist.append(line.strip(b'\n')) # make sure to use bytes here
        print(passlist)
Related