How to workaround `exist_ok` missing on Python 2.7?

One way around it is using pathlib. It has a backport for Python 2 and its mkdir() function supports exist_ok.

  from pathlib import Path
except ImportError:
  from pathlib2 import Path  # python 2 backport


As the comment suggests, use parents=True for makedirs().

Path(settings.STATIC_ROOT).mkdir(exist_ok=True, parents=True)

Leave a Comment