From 1e56bb95eb83f977a43e03cae3efaac8c2b10cff Mon Sep 17 00:00:00 2001 From: Wirawan Purwanto Date: Mon, 24 Feb 2014 16:06:09 -0500 Subject: [PATCH] * wpylib.file.file_utils: added path_prep and mkdir_p. --- file/file_utils.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/file/file_utils.py b/file/file_utils.py index a05a033..280e778 100644 --- a/file/file_utils.py +++ b/file/file_utils.py @@ -171,6 +171,27 @@ def path_split_all(p): return _pathsplit(p) +def path_prep(*paths): + """Like os.path.join, except that the directory part is created \ + on-the-fly as needed.""" + from os.path import dirname, isdir, join + path = join(*paths) + d = dirname(path) + mkdir_p(d) + return path + +def mkdir_p(name): + """A pure python implementation of my shell favorite `mkdir -p' command. + To conform to that command's behavior, we will not issue an error + if the file name exists and is a directory. + Returns 1 if new directories are made, returns -1 if nothing is done.""" + from os.path import isdir + if isdir(name): + return -1 + else: + os.makedirs(name) + return 1 + # - globbing