繁体   English   中英

目录名称和文件扩展名中每个字符串的os.rename(查找表)

[英]os.rename per string in dir name and fileextension (lookup table)

我在从查找表调用键和值来重命名文件时遇到麻烦。 任务:

  • 在CWD中,找到 =camID 结尾的每个目录(例如,... = d5),然后
  • =camID找到raw_file ,然后
  • 在所有raw_file文件名(而不是其他文件名)前添加device_name前缀。

代码

for camID in config:
    if dir_name.endswith(camID):
        for filename in os.listdir(camID):
            if filename.endswith(config(nested(raw_file))):
                os.rename(filename, config(nested(cam_name)){}_{}filename)

查找

config = {
    'g7': {},
    'd5': {},
}
config['g7']['cam_name'] = 'Canon-G7'
config['g7']['raw_file'] = ('cr2', 'jpg', 'mp4')

config['d5']['cam_name'] = 'Nikon-D5'
config['d5']['raw_file'] = ('nef', 'jpg', 'avi')

#'g7', 'd5' are called "camID"

之前之后的树:

CWD                                      
    01_camdirab=d5                       
          /aaa/ .nef,.jpg,.avi,.wav
    02_camdirxyz=g7                     
          /bbb/ddd/ .cr2,.jpg,.mp4
    04_camdire012345                     
          / .mp4,.jpg,.avi

CWD                                      
    01_camdirab=d5                       
          /aaa/ Nikon-D5_.nef, Nikon-D5_.jpg, Nikon-D5_.avi, .wav         
    02_camdirxyz=g7                      
          /bbb/ddd/ Canon-G7_.cr2, Canon-G7_.jpg, Canon-G7_.mp4              
    04_camdire012345                     
          /.mp4,.jpg,.avi      

有点hacky,但是这是该设置的工作原理:

import os

config = {
    'g7': {},
    'd5': {},
}
config['g7']['cam_name'] = 'Canon-G7'
config['g7']['raw_file'] = ('cr2', 'jpg', 'mp4')

config['d5']['cam_name'] = 'Nikon-D5'
config['d5']['raw_file'] = ('nef', 'jpg', 'avi')

root = "test"

for camID in config:
    for dir in next(os.walk(root))[1]:
        if dir.lower().endswith(camID):
            for path, dirs, files in os.walk(os.path.join(root, dir)):
                for f in files:
                    if any([f.lower().endswith(x) for x in config[camID]["raw_file"]]):
                        os.rename(os.path.join(path, f), 
                                  os.path.join(path, "%s_%s" % (config[camID]['cam_name'], f)))

请注意os.walk()仅用于获取目录,然后再次使用它来递归遍历整个子目录。

结果,我以此为起点:

# find test
test
test/.jpg
test/04_camdire012345
test/04_camdire012345/.avi
test/04_camdire012345/.jpg
test/04_camdire012345/.mp4
test/02_camdirxyz=g7
test/02_camdirxyz=g7/bbb
test/02_camdirxyz=g7/bbb/ddd
test/02_camdirxyz=g7/bbb/ddd/.mp4
test/02_camdirxyz=g7/bbb/ddd/.jpg
test/02_camdirxyz=g7/bbb/ddd/.cr2
test/01_camdirab=d5
test/01_camdirab=d5/aaa
test/01_camdirab=d5/aaa/.wav
test/01_camdirab=d5/aaa/.avi
test/01_camdirab=d5/aaa/.jpg
test/01_camdirab=d5/aaa/.nef

运行代码后:

# find test
test
test/.jpg
test/04_camdire012345
test/04_camdire012345/.avi
test/04_camdire012345/.jpg
test/04_camdire012345/.mp4
test/02_camdirxyz=g7
test/02_camdirxyz=g7/bbb
test/02_camdirxyz=g7/bbb/ddd
test/02_camdirxyz=g7/bbb/ddd/Canon-G7.cr2
test/02_camdirxyz=g7/bbb/ddd/Canon-G7.jpg
test/02_camdirxyz=g7/bbb/ddd/Canon-G7.mp4
test/01_camdirab=d5
test/01_camdirab=d5/aaa
test/01_camdirab=d5/aaa/Nikon-D5.nef
test/01_camdirab=d5/aaa/Nikon-D5.jpg
test/01_camdirab=d5/aaa/Nikon-D5.avi
test/01_camdirab=d5/aaa/.wav

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM