2

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
  • I think your first assumption about having to convert to utf8 is wrong ... Commented Feb 7, 2021 at 4:01

1 Answer 1

1

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)
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the response! I tried your recommendation but it seemed to give the same thing. Also, is there a different way I should go about it? When I try reading the file this way: def open_file2(): file2 = askopenfile(mode ='r', filetypes =[('Text Files', '*.txt')]) content2 = [] passlist = [] if file2 is not None: content2 = file2.readlines() #passlist = list(content2) for line in content2: passlist.append(line.strip('\n')) print(passlist)
i get: UnicodeDecodeError: 'charmap' codec can't decode byte 0x90 in position 2023: character maps to <undefined>
passes is not defined ... content2 appears to be the text content of the file ... ignore the utf8 comment as that is unlikely your issue .... you need to split the content2 using content2.splitlines() to return individual lines instead of every character (not sure of the format of these files ... you might need to do content2.split("something") other than lines
based on your error it actually seems like it has alot to do with your decode call ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.