os.rename 目录名称和文件扩展名中的每个字符串(查找表) [英] os.rename per string in dir name and fileextension (lookup table)

查看:80
本文介绍了os.rename 目录名称和文件扩展名中的每个字符串(查找表)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am having trouble calling keys and values from lookup table to rename files. The task:

  • 在 CWD 中,找到每个结尾 =camID(例如,...=d5)的目录,然后
  • =camID中找到raw_file,然后
  • 使用 device_name 为所有 raw_file 文件名(但不是其他文件名)添加前缀.
  • in CWD, find each dir that ends =camID (e.g, ...=d5), then
  • find raw_file inside =camID, then
  • prefix all raw_file filenames (but not other filenames) with 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,但以下是适用于该设置的方法:

kinda hacky, but here's what works on that setup:

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()的用法是只获取目录,然后再次使用它递归遍历整个子目录.

Please note the usage of os.walk() to get directories only, and then using it again to recursively walking through the whole subdirectory.

因此,我以此为起点:

# 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

这篇关于os.rename 目录名称和文件扩展名中的每个字符串(查找表)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆