]> git.phdru.name Git - m_lib.git/blob - m_lib/m_shutil.py
Replace <> with != for Py3 compatibility
[m_lib.git] / m_lib / m_shutil.py
1 #! /usr/bin/env python
2 """
3 Shell utilities. Additional to shutil.py (standard library module).
4 """
5
6
7 from __future__ import print_function
8 import os, string
9
10
11 mkhier_error = "m_shutil.mkhier_error"
12
13 def mkhier(path): # Python implementation of UNIX' mkdir -p /path/to/dir
14    if os.path.isdir(path):
15       return # It's Ok to have the directory already created
16
17    if os.path.exists(path):
18       raise mkhier_error, "`%s' is file" % path
19
20    list_dirs = string.split(path, os.sep)
21    #print(list_dirs)
22    for i in range(0, len(list_dirs)):
23       new_path = string.join(list_dirs[0:i+1], os.sep)
24       if (new_path != '') and (not os.path.exists(new_path)):
25          #print("Making", new_path)
26          os.mkdir(new_path)
27
28
29 def mcd(dir):
30    os.mkdir(dir)
31    os.chdir(dir)
32
33
34 def test():
35    mkhier("I.AM/creating/TEST/dir")
36
37 if __name__ == "__main__":
38    test()