Index: Doc/library/os.rst =================================================================== --- Doc/library/os.rst (revision 59590) +++ Doc/library/os.rst (working copy) @@ -1007,17 +1007,20 @@ .. versionadded:: 2.3 -.. function:: mkdir(path[, mode]) +.. function:: mkdir(path[, mode[, excl=True]]) Create a directory named *path* with numeric mode *mode*. The default *mode* is ``0777`` (octal). On some systems, *mode* is ignored. Where it is used, the current umask value is first masked out. Availability: Macintosh, Unix, Windows. + By default, if the directory already exists, :exc:`OSError` is thrown. If excl is + False, it is not an error for the directory to exist already. + It is also possible to create temporary directories; see the :mod:`tempfile` module's :func:`tempfile.mkdtemp` function. -.. function:: makedirs(path[, mode]) +.. function:: makedirs(path[, mode[, excl=True]]) .. index:: single: directory; creating @@ -1029,6 +1032,10 @@ created. The default *mode* is ``0777`` (octal). On some systems, *mode* is ignored. Where it is used, the current umask value is first masked out. + By default, if the directory already exists, :exc:`OSError` is thrown. If + excl is False, it is not an error for the directory to exist already. It + is never an error for ancestor directories to exist already. + .. note:: :func:`makedirs` will become confused if the path elements to create include Index: Lib/os.py =================================================================== --- Lib/os.py (revision 59590) +++ Lib/os.py (working copy) @@ -146,29 +146,39 @@ # Super directory utilities. # (Inspired by Eric Raymond; the doc strings are mostly his) +_mkdir = mkdir -def makedirs(name, mode=0777): - """makedirs(path [, mode=0777]) +def mkdir(name, mode=0777, excl=True): + """mkdir(path [, mode=0777 [, excl=True]]) + + Create the named directory in the indicated mode. By default, if the + directory already exists, OSError is thrown. If excl is False, it is + not an error for the directory to exist already. + """ + try: + _mkdir(name, mode) + except OSError, e: + if excl or not (e.errno == errno.EEXIST and path.isdir (name)): + raise +def makedirs(name, mode=0777, excl=True): + """makedirs(path [, mode=0777 [, excl=True]]) + Super-mkdir; create a leaf directory and all intermediate ones. Works like mkdir, except that any intermediate path segment (not just the rightmost) will be created if it does not exist. This is - recursive. - + recursive. By default, if the directory already exists, OSError is + thrown. If excl is False, it is not an error for the directory to exist + already. It is never an error for ancestor directories to exist already. """ head, tail = path.split(name) if not tail: head, tail = path.split(head) if head and tail and not path.exists(head): - try: - makedirs(head, mode) - except OSError, e: - # be happy if someone already created the path - if e.errno != errno.EEXIST: - raise + makedirs(head, mode, excl=False) if tail == curdir: # xxx/newdir/. exists if xxx/newdir exists return - mkdir(name, mode) + mkdir(name, mode, excl) def removedirs(name): """removedirs(path)