How can I safely create a nested directory in Python?

On Python ≥ 3.5, use pathlib.Path.mkdir: For older versions of Python, I see two answers with good qualities, each with a small flaw, so I will give my take on it: Try os.path.exists, and consider os.makedirs for the creation. As noted in comments and elsewhere, there’s a race condition – if the directory is created … Read more