0

This is my code: import easygui

while True:
    try:
        print('Select your file')
        proxyfile = easygui.fileopenbox('', 'Select your file')
        proxylines = proxyfile.splitlines()
        proxylinesamount = len(open(proxylines).readlines())
        break

    except (TypeError, AttributeError) as e:
        print(f'Error. File isn\'t valid. Reason: {e}')
        continue

For some reason it returns the following error:

expected str, bytes or os.PathLike object, not list

I understand that this is a TypeError but I simply can't understand what I'm doing wrong

5
  • 2
    proxylines is a list and you're trying to open it as a file. Commented Mar 18, 2021 at 16:16
  • what was the purpose of proxyfile.splitlines()? Commented Mar 18, 2021 at 16:19
  • @PApostol any idea on how I can convert it to a string? Commented Mar 18, 2021 at 16:21
  • @llamaCaraDara oops i forgot to remove that part. It has another purpose later on in the script. Commented Mar 18, 2021 at 16:22
  • it looks like you want to use len(open(proxyfile).readlines()). from what i'm reading in easygui fileopenbox returns a file path Commented Mar 18, 2021 at 16:27

1 Answer 1

2

I would assume you wanted this:

import easygui

while True:
    try:
        print('Select your file')
        proxy_file = easygui.fileopenbox('', 'Select your file')  # Type: str
        with open(proxy_file, 'r') as fh:
            proxy_lines = list(fh)
            proxy_lines_amount = len(proxy_lines)
        break  # i'm not sure if while True and break are viable here

    except (TypeError, AttributeError) as e:
        print(f'Error. File isn\'t valid. Reason: {e}')
        continue
Sign up to request clarification or add additional context in comments.

3 Comments

I think list(fh) is more usually done as fh.readlines()
@Barmar sure thing! However I prefer it this way. don't... beat me.. please?
No problem, but consider yourself on notice :)

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.