我正在尝试根据嵌套字典将特定文件类型移动到新目录。
例如,(为了分隔视频和照片文件)将config[key1][other_file]
中定义的媒体文件移动到OTHERMEDIA
,用于各自文件夹中的每个相机型号:
001__=d5
OTHERMEDIA #move files to here
IMAGES
[mix of mediafiles in subdirectories] #move files from here
002__=alpha9
OTHERMEDIA #move files to here
IMAGES
[mix of mediafiles in subdirectories] #move files from here
代码提供shutil.move(os.path.join(root,files), os.path.join(destinationpath,files))
。我做错了什么?
import shutil
import os
config = {
'd5': {},
'alpha9': {},
'g7': {},
}
config['d5']['other_file'] = ('avi', 'AVI')
config['alpha9']['other_file'] = ('jpg', 'JPG')
config['g7']['other_file'] = ('mp4', 'MP4')
destinationpath = 'OTHERMEDIA'
root = os.getcwd()
for camID in config:
for dir in next(os.walk(root))[1]:
if dir.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]["other_file"]]):
os.path.join(path, f). files, os.path.join(destinationpath,files)
答案 0 :(得分:0)
当你应该os.path.join(path, files)
时,你正在做os.path.join(path, f)
。 files
是整个可迭代的; f
是特定文件。
此外,另外,由于您正在查看f.lower()
,因此您无需将其与文件扩展名的大写版本进行比较。