基于字典将选定文件类型移动到新目录

2024-10-03 21:32:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试将特定的文件类型移动到基于嵌套字典的新目录中。你知道吗

例如,(分离视频和照片文件)将每个相机型号的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)

Tags: pathinconfigformovehereosroot
1条回答
网友
1楼 · 发布于 2024-10-03 21:32:09

你在做os.path.join(path, files),而你应该做os.path.join(path, f)files是整个iterable;f是特定文件。你知道吗

另外,由于您正在查看f.lower(),因此不需要将其与文件扩展名的大写版本进行比较。你知道吗

相关问题 更多 >