操作系统重命名目录名和文件扩展名中的每字符串(查找表)

2024-04-19 16:05:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我无法从查找表中调用键和值来重命名文件。任务:

  • 在CWD中,找到结束的每个dir=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      

Tags: nameconfigrawfilenamefilejpgmp4avi
1条回答
网友
1楼 · 发布于 2024-04-19 16:05:08

有点不对劲,但在这个设置上有一些有用的东西:

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

相关问题 更多 >