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.
if not os.path.exists(directory):
os.makedirs(directory)
As noted in comments and elsewhere, there's a race condition - if the directory...