How to create directories and sub directories efficiently and elegantly in Python 2.7?

此生再无相见时 提交于 2019-12-22 10:20:00

问题


I am trying to create a bunch of directories and sub directories at a specific location in my PC. My process is something like this:

  1. Check if there's any directory with the same directory name. Skip if so.
  2. If not, create the directory and the pre-defined sub directories under that directory.

This is the code I came up with using os module:

def Test():
    main_dir = ["FolderA", "FolderB"] 
    common_dir = ["SubFolder1", "SubFolder2", "SubFolder3"]

    for dir1 in main_dir:
        if not os.path.isdir(dir1):
            for dir2 in common_dir:
                os.makedirs("%s/%s" %(dir1,dir2))

I am wondering if there's any better way to do this very same task (probably shorter, more efficient and more pythonic)?


回答1:


Python follows the philosophy

It is better to ask for forgiveness than to ask for permission.

So rather than checking isdir, you would simply catch the exception thrown if the leaf directory already exists:

def Test():
    main_dir = ["FolderA", "FolderB"] 
    common_dir = ["SubFolder1", "SubFolder2", "SubFolder3"]

    for dir1 in main_dir:
        for dir2 in common_dir:
            try: os.makedirs(os.path.join(dir1,dir2))
            except OSError: pass

You can also replace string interpolation "%s/%s" %(dir1,dir2) with os.path.join(dir1, dir2)

Another more succinct way is to do the cartesian product instead of using two nested for-loops:

for dir1, dir2 in itertools.product(main_dir, common_dir):
    try: os.makedirs(os.path.join(dir1,dir2))
    except OSError: pass



回答2:


How about:

import os
from itertools import starmap

def Test():
    main_dir = ["FolderA", "FolderB"] 
    common_dir = ["SubFolder1", "SubFolder2", "SubFolder3"]

    map(os.makedirs, starmap(os.path.join, zip(main_dir, common_dir)))

And if we're worried about os.makedirs() throwing errors:

import os
from itertools import starmap

def safe_makedirs(*args):
    try:
        return os.makedirs(*args)
    except:
        pass  # Ignore errors; for example if the paths already exist!

def Test():
    main_dir = ["FolderA", "FolderB"] 
    common_dir = ["SubFolder1", "SubFolder2", "SubFolder3"]

    map(safe_makedirs, starmap(os.path.join, zip(main_dir, common_dir)))


来源:https://stackoverflow.com/questions/30837089/how-to-create-directories-and-sub-directories-efficiently-and-elegantly-in-pytho

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!