根据查找表字符串匹配创建子目录

时间:2017-11-07 23:21:35

标签: python python-3.x

如何仅在匹配查找表中的字符串的目录中创建子目录。它是否适用于os.path.join

例如,在 =d5=g7=a9结尾的cwd目录中,应创建两个目录:{{1 }和_raw_files。如何指出_sup_files根据查找表字符串生成(例如os.path.join ...)?

之前构建并在之后构建目录:

config['g7']

我的代码和查找表:

CWD                                      
    01_camdirab=d5                       #<--create new dirs inside this
          /subdirs/.nef,.jpg,.avi
    02_camdirxyz=g7                      #<--create new dirs inside this
          /subdirs/.cr2,.jpg,.mp4
    03_camdire012345=a9                  #<--create new dirs inside this
          /subdirs/.mp4,.jpg,.avi
    04_camdire012345                     #<--DO NOT create new dirs inside (no string) this
          /subdirs/.mp4,.jpg,.avi


CWD
    01_camdirab=d5                       
          /_raw_files
          /_sup_files
          /subdirs/.nef,.jpg,.avi
    02_camdirxyz=g7                    
          /_raw_files
          /_sup_files
          /subdirs/.cr2,.jpg,.mp4
    03_camdire012345=a9                  
          /_raw_files
          /_sup_files
          /subdirs/.mp4,.jpg,.avi
    04_camdire012345                     
          /subdirs/.mp4,.jpg,.avi

1 个答案:

答案 0 :(得分:1)

试试这个:

def createDir(path):
    if not os.path.exists(path):
        os.mkdir(path)

import os
cwd = os.getcwd()

config = {
    'g7': {},
    'd5': {},
    'a9': {},
}    
config['g7']['append_dir_to_filename'] = 1
config['g7']['device_name'] = 'Canon-G7'
config['g7']['raw_file'] = ('cr2', 'jpg', 'mp4')
config['d5']['append_dir_to_filename'] = 1
config['d5']['device_name'] = 'Nikon-D5'
config['d5']['raw_file'] = ('nef', 'jpg', 'avi')
config['a9']['append_dir_to_filename'] = 1
config['a9']['device_name'] = 'Sony-alpha-a9'
config['a9']['raw_file'] = ('mp4', 'jpg', 'avi')

dirs_name = os.listdir(cwd)
for dir_name in dirs_name:
    for key_word in config:
        if dir_name.endswith(key_word):
            _raw_files = os.path.join(cwd, dir_name, r'_raw_files')
            _sup_files = os.path.join(cwd, dir_name, r'_sup_files')
            createDir(_raw_files)
            createDir(_sup_files)