#StackBounty: #python #resources #packaging Is this the approved way to get data adjacent to a Python script?

Bounty: 300

I have a Python script that needs some data that’s stored in a file that will always be in the same location as the script. I came up with this method to get it. This script isn’t part of a module. It’s all one file. Is this the correct way?

def fetch_wordlist():
    wordlist = 'wordlist.txt'
        import importlib.resources as res
        return res.read_binary(__file__, wordlist)
    except ImportError:
        import pkg_resources as resources
        req = resources.Requirement.parse('makepw')
        wordlist = resources.resource_filename(req, wordlist)
    except ImportError:
        import os.path
        wordlist = os.path.join(os.path.dirname(__file__), wordlist)
    with open(wordlist, 'rb') as f:
        return f.read()

This seems ridiculously complex. Also, it seems to rely on the package management system in ways I’m uncomfortable with. The script no longer works unless it’s been pip-installed, and that also doesn’t seem desirable.

Get this bounty!!!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.