2
\$\begingroup\$

Bit of a basic question but I was creating a ancestor/family tree app thingy (haven't really worked out the details) and I wrote a small start-up program (seen below) which checks the AppData directory exists and the config file exist (consisting of the number of tree and their respective names).

I was wondering if it's better to include the code that updates the config file at the beginning of runtime EVERY TIME or not since its only there in case the user accidently changes something:

import json
import os

AncestoryDir = os.getenv("APPDATA") + "\Ancestry"
ConfigFile = AncestoryDir + "\CONFIG.ini"

# Make Ancestory Directory
if not os.path.exists(AncestoryDir):
    os.makedirs(AncestoryDir)

# Update Config.ini File
CONFIGTree = {"Tree Count": None, "Tree's": []}
for File in os.scandir(AncestoryDir):
    if File.is_file():
        Tree, Suffix = File.name.split(".")
        if Suffix == "json":
            CONFIGTree["Tree's"].append(Tree)
CONFIGTree["Tree Count"] = len(CONFIGTree["Tree's"])
with open(ConfigFile, "w") as JSON:
    json.dump(CONFIGTree, JSON, indent = 4)

Main File:

import os

os.system("python Ancestory.py")
#Other Stuff

As you can see it updates the config file EVERY TIME and im not sure if its worth it. Any thoughts or suggestions are appreciated.

Note: Please don't tell me about PEP-8, I'm well aware of it and I choose to ignore it out of pure idiocy (my choice)

\$\endgroup\$
  • 1
    \$\begingroup\$ Sure, it's your choice, but... there's a reason it exists. A third-party dev looking at AncestoryDir with no other context would assume that it's a class. \$\endgroup\$ – Reinderien Nov 11 at 21:46
  • 2
    \$\begingroup\$ So your request that we please don't tell you about PEP8 might go unheeded; all constructive feedback is on-topic. \$\endgroup\$ – Reinderien Nov 11 at 21:48
2
\$\begingroup\$

Pathlib

AncestoryDir = os.getenv("APPDATA") + "\Ancestry"
ConfigFile = AncestoryDir + "\CONFIG.ini"

# Make Ancestory Directory
if not os.path.exists(AncestoryDir):
    os.makedirs(AncestoryDir)

can reduce to

ancestry_dir = Path(os.getenv('APPDATA')) / 'Ancestry'
config_file = ancestry_dir / 'CONFIG.ini'

ancestry_dir.mkdir(exist_ok=True)

Note:

  • pathlib grants you a cleaner, object-oriented method for path manipulation
  • it's spelled "ancestry"
  • with pathlib there's no need for a separate existence check
  • even if you kept your approach, you have an invalid escape \A that either needs to be double-escaped or exist in a raw string

pathlib applies to your other code, too. Also it's spelled "Trees", not "Tree's".

PEP8

Protest thou may, but this actually matters. Coding is a form of communication, and the less we successfully communicate with other coders, the less they're able to help and work with us.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ I can't believe I've been spelling it wrong lol, thanks :). Also 'Path' not defined? Even with pathlib \$\endgroup\$ – DAda Nov 12 at 9:19
  • \$\begingroup\$ Can you show your import statement? \$\endgroup\$ – Reinderien Nov 12 at 14:33
  • \$\begingroup\$ And what version of Python are you running? \$\endgroup\$ – Reinderien Nov 12 at 14:33
  • \$\begingroup\$ python version 3.8.6 \$\endgroup\$ – DAda Nov 12 at 14:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.