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?
settings.pyand import them when needed{'IMG_DIR': 'images', 'HASH_DIR': 'hashes'}etc.