Use os.makedirs() rather than os.mkdir() to create all intermediate dirs in the path.