如何根据开/关开关将父目录的名称附加到文件名中,并将文件扩展名存储在查找表中?
例如,如果目录以... =d5
结尾,则根据查找表,所有raw_file
(nef, jpg, avi
)文件名< strong>在 ... =d5
目录中附加1级父目录名称,如dirname_origfilename.nef
。如果是['append_dir_to_filename'] = 0
,则不要附加目录名称。
文件名模式之前和之后:
CWD
01_camdirab=d5
/aaa/ .nef,.jpg,.avi,.wav
02_camdirxyz=g7
/bbb/ddd/ .cr2,.jpg,.mp4
03_camdire012345=a9
/yyy/kkk/xxx/ .mp4,.jpg,.avi,.thm
04_camdire012345
/ .mp4,.jpg,.avi
CWD
01_camdirab=d5
/aaa/aaa_.nef, aaa_.jpg, aaa_.avi, .wav #appended raw files
02_camdirxyz=g7
/bbb/ddd/.cr2,.jpg,.mp4 #not appended, 'append_dir_to_filename'] = 0
03_camdire012345=a9
/yyy/kkk/xxx/ xxx_.mp4, xxx_.jpg, xxx_.avi, .thm #appended raw files
04_camdire012345
/.mp4,.jpg,.avi #not appended, dir not in lookup,
代码(无法正确链接到查找表):
import os
import glob
import shutil
cwd = os.getcwd()
config = {
'append_dir_to_filename' : ('d5', 'a9'),
'd5': ('nef', 'jpg', 'avi'),
'a9': ('mp4', 'jpg', 'avi')
}
for root, dirs, files in os.walk(cwd):
prefix = os.path.basename(root)
if (prefix[-2:] in config['append_dir_to_filename']):
for f in files:
cameraObj = config[prefix[-2:]]
for extension in cameraObj.supported_extensions:
if (f.endswith('.' + extension)):
os.rename(os.path.join(root, f), os.path.join(root, "{}_{}".format(prefix, f)))
1台摄像机的实际查找表条目。有10个摄像头,所以10倍。
config['d5']['append_dir_to_filename'] = 1
config['d5']['device_name'] = 'Nikon-D5'
config['d5']['raw_file'] = ('nef', 'jpg', 'avi')
config['d5']['sup_file'] = ('jpg', 'wav')
config['d5']['to_else_file'] = ('avi')
config['d5']['timestamp'] = ('lwt')
config['d5']['md5'] = 1
config['d5']['code1'] = 'FAR'
config['d5']['gps'] = 1
答案 0 :(得分:1)
您实际上没有在代码中显示使用查找表的部分,但是您可以通过重构循环表来简化您的问题。像这样:
config = {
'append_dir_to_filename' : ('d5', 'a9'),
'd5': ('nef', 'jpg', 'avi'),
'a9': ('mp4', 'jpg', 'avi')
}
这些修改假定您不需要有关您不想附加到文件名的目录的信息。有了这个,其余的代码非常简单:
prefix = os.path.basename(root)
if (prefix[-2:] in config['append_dir_to_filename']):
for f in files:
for extension in config[prefix[-2:]]:
if (f.endswith('.' + extension)):
#Do the appending business here
还没有真正测试过代码,但这应该对你有用。
编辑:如果您按照我的评论中的建议进行操作,请使用以下代码:
prefix = os.path.basename(root)
if (prefix[-2:] in config['append_dir_to_filename']):
for f in files:
cameraObj = config[prefix[-2:]]
for extension in cameraObj.supported_extensions:
if (f.endswith('.' + extension)):
#Do the appending business here
,查找表就像这样:
config = {
'append_dir_to_filename' : ('d5', 'a9'),
'd5': Camera(...list of properties...),
'a9': Camera(...list of properties...)
}
编辑:好的,我基本上必须从头开始编写整个代码。这个版本肯定有效。
import os
cwd = os.getcwd()
config = {
'append_dir_to_filename' : ('d5', 'a9'),
'd5': ('nef', 'jpg', 'avi'),
'a9': ('mp4', 'jpg', 'avi')
}
cameraDirs = [os.path.join(cwd, x) for x in next(os.walk(cwd))[1] if x[-2:] in config['append_dir_to_filename']]
for cameraDir in cameraDirs:
cameraShortName = cameraDir[-2:]
for rootDir, _, files in os.walk(cameraDir):
prefix = os.path.basename(rootDir)
for file in files:
if (any(x for x in config[cameraShortName] if file.endswith(x))):
os.rename(os.path.join(rootDir, file), os.path.join(rootDir, "{}_{}".format(prefix, file)))