0

I'm trying to create a bunch of directories. Some of them will fail due to permissions and whatnot and in that case, I would like that directory created in /tmp and then replace the value in the corresponding variable.

import os

ROOT = "/var"
IMG_DIR = "images"
HASH_DIR = "hashes"
TEAM_DIR = "teams"

for some_dir in [IMG_DIR, HASH_DIR, TEAM_DIR]:
    current_path = os.path.join(ROOT, some_dir)
    if not os.path.exists(current_path):
        try:
            os.makedirs(current_path)
        except OSError:
            new_path = os.path.join("/tmp", some_dir)
            # if the error happened in images, update IMG_DIR with the new path
            # if the error happened in hashes, update HASH_DIR with the new path
            # if the error happened in teams, update TEAM_DIR with the new path

IMG_DIR, HASH_DIR and TEAM_DIR will be global vars and are accessed throughout the program. What's the most elegant/Pythonic way to accomplish this?

2
  • I'd suggest to place them in settings.py and import them when needed Commented Jan 10, 2019 at 16:31
  • 2
    start with a dict instead, e.g. {'IMG_DIR': 'images', 'HASH_DIR': 'hashes'} etc. Commented Jan 10, 2019 at 16:32

1 Answer 1

2

You need something like that:

import os

ROOT = "/var"
my_dirs = {
    'IMG_DIR': "images",
    'HASH_DIR': "hashes",
    'TEAM_DIR': "teams"
}

for name, path in my_dirs.items():
    current_path = os.path.join(ROOT, path)
    if not os.path.exists(current_path):
        try:
            os.makedirs(current_path)
        except OSError:
            new_path = os.path.join("/tmp", path)
            my_dirs[name] = new_path 

UPD

If you strictly require update the variables and do not accept dictionary (Why??), you can use following approach:

import os

def create_dir(root: str, path: str) -> str:
    current_path = os.path.join(root, path)
    if not os.path.exists(current_path):
        try:
            os.makedirs(current_path)
        except OSError:
            new_path = os.path.join("/tmp", path)
            # maybe create this directory here?
            return new_path 
    return current_path

ROOT = "/var"
IMG_DIR =  create_dir(ROOT, "images")
HASH_DIR = create_dir(ROOT, "hashes")
TEAM_DIR = create_dir(ROOT, "teams")
Sign up to request clarification or add additional context in comments.

2 Comments

I thought about this approach, but the statement my_dirs[name] = current_path only updates the value of the key IMG_DIR within the dictionary my_dirs. The value of the variable IMG_DIR does not change and that's my initial problem. Also I think you mean my_dirs[name] = new_path
Thanks - I'm editing someone else's code where variables are scattered a few hundred times all over the place, which is why I'm hesitant to use dicts.

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.