Where to put a config file for a compiled Python script?

后端 未结 4 1918
囚心锁ツ
囚心锁ツ 2021-01-21 02:51

This is similar to Where to put a configuration file in Python?, but I\'m asking about scripts compiled/frozen with py2exe or similar on Windows systems. (Namely, this one: Wha

4条回答
  •  南笙
    南笙 (楼主)
    2021-01-21 03:21

    I've created "compiled" executables in python on widnows that have files types associated with it, and I never had any issues opening my config file which was placed in the same location as the .exe. I used something like this to find it:

        conf = open(r"%s\settings.conf" % os.getcwd())
    

    I stored my values separated by a newline character, so the file looked like this:

    PNelson
    21
    6'
    

    This stores a username, age, and height, to read the values I would use something like this:

    settings = "\n".split(conf.read())
    

    I'd then check the length of the array to make sure the config file wasn't corrupted or some way, if it was I'd write default values to the file and use those.

提交回复
热议问题