Elegant way to make all dirs in a path

前端 未结 3 744
暗喜
暗喜 2021-02-04 23:58

Here are four paths:

p1=r\'\\foo\\bar\\foobar.txt\'
p2=r\'\\foo\\bar\\foo\\foo\\foobar.txt\'
p3=r\'\\foo\\bar\\foo\\foo2\\foobar.txt\'
p4=r\'\\foo2\\bar\\foo\\fo         


        
3条回答
  •  野性不改
    2021-02-05 00:20

    A simple way to build the paths in POSIX systems. Assume your path is something like: dirPath = '../foo/bar', where neither foo or bar exist:

    path = ''
    for d in dirPath.split('/'):
       # handle instances of // in string
       if not d: continue 
    
       path += d + '/'
       if not os.path.isdir(path):
          os.mkdir(path)
       
    

提交回复
热议问题