def mkdir (path, mode=0777): """Create a directory, but report no error if it already exists. This is the same as os.mkdir except it doesn't complain if the directory already exists. It works by calling os.mkdir. In the event of an error, it checks if the requested path is a directory and suppresses the error if so. """ try: os.mkdir (path, mode) except OSError: if not os.path.isdir (path): raise def makedirs (path, mode=0777): """Create a directory recursively, reporting no error if it already exists. This is like os.makedirs except it doesn't complain if the specified directory or any of its ancestors already exist. This works by essentially re-implementing os.makedirs but calling the mkdir in this module instead of os.mkdir. This also corrects a race condition in os.makedirs. """ if not os.path.isdir (path): head, tail = os.path.split (path) if not tail: head, tail = os.path.split (head) if head and tail: makedirs (head, mode) mkdir (path, mode)