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
.
try:
from pathlib import Path
except ImportError:
from pathlib2 import Path # python 2 backport
Path(settings.STATIC_ROOT).mkdir(exist_ok=True)
As the comment suggests, use parents=True
for makedirs()
.
Path(settings.STATIC_ROOT).mkdir(exist_ok=True, parents=True)
You could call makedirs() after checking that the path does not exist:
import os
if not os.path.exists(path):
os.makedirs(path)