Make directory path: Difference between revisions

From Rosetta Code
Content added Content deleted
No edit summary
No edit summary
Line 52: Line 52:
def mkdirp(path):
def mkdirp(path):
os.makedirs(path, exist_ok=True)
os.makedirs(path, exist_ok=True)
</lang>


=={{header|JavaScript}}==
{{works with|Node.js}}

Largely stolen from the popular [https://www.npmjs.org/package/mkdirp mkdirp library].

<lang Javascript>var path = require('path');
var fs = require('fs');

function mkdirp (p, f, made) {
if (!made) made = null;

var cb = f || function () {};
p = path.resolve(p);

fs.mkdir(p, function (er) {
if (!er) {
made = made || p;
return cb(null, made);
}
switch (er.code) {
case 'ENOENT':
mkdirp(path.dirname(p), function (er, made) {
if (er) cb(er, made);
else mkdirp(p, cb, made);
});
break;

// In the case of any other error, just see if there's a dir
// there already. If so, then hooray! If not, then something
// is borked.
default:
fs.stat(p, function (er2, stat) {
// if the stat fails, then that's super weird.
// let the original error be the failure reason.
if (er2 || !stat.isDirectory()) cb(er, made)
else cb(null, made);
});
break;
}
});
}
</lang>
</lang>

Revision as of 05:01, 9 August 2014

Make directory path is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

Given a path to a directory (for example ./path/to/dir) create the directory and any missing parents. If the directory already exists, return successfully.

This task is named after the posix mkdir -p command, and several libraries which implement the same behavior. Ideally implementations will work equally well cross-platform (on windows, linux, and OS X).


Python

<lang Python> from errno import EEXIST from os import mkdir, curdir from os.path import split, exists

def mkdirp(path, mode=0777):

   head, tail = split(path)
   if not tail:
       head, tail = split(head)
   if head and tail and not exists(head):
       try:
           mkdirp(head, mode)
       except OSError as e:
           # be happy if someone already created the path
           if e.errno != EEXIST:
               raise
       if tail == curdir:  # xxx/newdir/. exists if xxx/newdir exists
           return
   try:
       mkdir(path, mode)
   except OSError as e:
       # be happy if someone already created the path
       if e.errno != EEXIST:
           raise

</lang>

Above is a modified version of the standard library's os.makedirs, for pedagogical purposes. In practice, you would be more likely to use the standard library call:

<lang Python> def mkdirp(path):

   try:
       os.makedirs(path)
   except OSError as exc: # Python >2.5
       if exc.errno == errno.EEXIST and os.path.isdir(path):
           pass
       else: raise

</lang>

In Python3 this becomes even simpler:

<lang Python> def mkdirp(path):

   os.makedirs(path, exist_ok=True)

</lang>


JavaScript

Works with: Node.js

Largely stolen from the popular mkdirp library.

<lang Javascript>var path = require('path'); var fs = require('fs');

function mkdirp (p, f, made) {

   if (!made) made = null;
   var cb = f || function () {};
   p = path.resolve(p);
   fs.mkdir(p, function (er) {
       if (!er) {
           made = made || p;
           return cb(null, made);
       }
       switch (er.code) {
           case 'ENOENT':
               mkdirp(path.dirname(p), function (er, made) {
                   if (er) cb(er, made);
                   else mkdirp(p, cb, made);
               });
               break;
               // In the case of any other error, just see if there's a dir
               // there already.  If so, then hooray!  If not, then something
               // is borked.
           default:
               fs.stat(p, function (er2, stat) {
                   // if the stat fails, then that's super weird.
                   // let the original error be the failure reason.
                   if (er2 || !stat.isDirectory()) cb(er, made)
                   else cb(null, made);
               });
               break;
       }
   });

} </lang>