我正在尝试将特定的文件类型移动到基于嵌套字典的新目录中。你知道吗
例如,(分离视频和照片文件)将每个相机型号的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)
你在做
os.path.join(path, files)
,而你应该做os.path.join(path, f)
。files
是整个iterable;f
是特定文件。你知道吗另外,由于您正在查看
f.lower()
,因此不需要将其与文件扩展名的大写版本进行比较。你知道吗相关问题 更多 >
编程相关推荐