Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
922 views
in Technique[技术] by (71.8m points)

python - os.rename per string in dir name and fileextension (lookup table)

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

  • 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.

Code:

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)

Lookup:

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"

Tree before and after:

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      
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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)))

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

As a result, I have this as a starting point:

# 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

And after running the code:

# 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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...