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