Python 2.7如何高效优雅的创建目录和子目录?
How to create directories and sub directories efficiently and elegantly in Python 2.7?
我想在我电脑的特定位置创建一堆目录和子目录。我的过程是这样的:
- 检查是否有同名目录。如果是,请跳过。
- 如果没有,则在该目录下创建目录和预定义的子目录。
这是我使用 os
模块想出的代码:
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))
我想知道是否有更好的方法来完成同样的任务(可能更短、更高效、更 pythonic)?
Python 遵循哲学
It is better to ask for forgiveness than to ask for permission.
因此,与其检查 isdir
,不如捕获叶目录已经存在时抛出的异常:
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
您还可以将字符串插值 "%s/%s" %(dir1,dir2)
替换为 os.path.join(dir1, dir2)
另一种更简洁的方法是做笛卡尔积而不是使用两个嵌套的 for 循环:
for dir1, dir2 in itertools.product(main_dir, common_dir):
try: os.makedirs(os.path.join(dir1,dir2))
except OSError: pass
怎么样:
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)))
如果我们担心 os.makedirs()
抛出错误:
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)))
我想在我电脑的特定位置创建一堆目录和子目录。我的过程是这样的:
- 检查是否有同名目录。如果是,请跳过。
- 如果没有,则在该目录下创建目录和预定义的子目录。
这是我使用 os
模块想出的代码:
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))
我想知道是否有更好的方法来完成同样的任务(可能更短、更高效、更 pythonic)?
Python 遵循哲学
It is better to ask for forgiveness than to ask for permission.
因此,与其检查 isdir
,不如捕获叶目录已经存在时抛出的异常:
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
您还可以将字符串插值 "%s/%s" %(dir1,dir2)
替换为 os.path.join(dir1, dir2)
另一种更简洁的方法是做笛卡尔积而不是使用两个嵌套的 for 循环:
for dir1, dir2 in itertools.product(main_dir, common_dir):
try: os.makedirs(os.path.join(dir1,dir2))
except OSError: pass
怎么样:
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)))
如果我们担心 os.makedirs()
抛出错误:
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)))