Preempt Lookup Table Rule In File Rename
The code below prefix select filenames with parent directory name per lookup table rules. It works perfectly. Yet, how I insert into this code an exception, so any and all files t
Solution 1:
There you go, this will make sure that directories with camera names ending won't be appended to the files. Just before you try to rename the file, you need to perform an additional check, which is done by the below code fragment.
prefix[-2:] not in config['append_dir_to_filename']
This simply checks if the current directory being considered is not part of the camera directories (1st level directories) defined in the look-up table above. The complete code is as below:
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 innext(os.walk(cwd))[1] if x[-2:] inconfig['append_dir_to_filename']]
for cameraDir in cameraDirs:
cameraShortName = cameraDir[-2:]
for rootDir, _, files inos.walk(cameraDir):
prefix = os.path.basename(rootDir)
for file in files:
if (any(x for x inconfig[cameraShortName] if file.endswith(x)) and prefix[-2:] notinconfig['append_dir_to_filename']):
os.rename(os.path.join(rootDir, file), os.path.join(rootDir, "{}_{}".format(prefix, file)))
Post a Comment for "Preempt Lookup Table Rule In File Rename"