2017-11-16 4 views
0

ネストされた辞書に基づいて特定のファイルタイプを新しいディレクトリに移動しようとしています。がshutil.move(os.path.join(root,files), os.path.join(destinationpath,files))与えファイルタイプを辞書に基づいて新しいディレクトリに移動する

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 

コード:

は、たとえばについては、(別々のビデオや写真ファイルには)それぞれのフォルダ内の各カメラモデルのためOTHERMEDIAconfig[key1][other_file]で定義されたメディアファイルを移動します。私が間違っていることは何ですか?

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

あなたがos.path.join(path, f)をやるべきときos.path.join(path, files)をやっています。 filesはiterable全体です。 fは特定のファイルです。

また、別として、f.lower()を見ているので、ファイル拡張子の大文字のバージョンと比較する必要はありません。

+0

が推奨通りに更新されました(OPを参照)。エラー: 'line 20、in os.path.join(path、f)。ファイル、os.path.join(destinationpath、files) AttributeError: 'str'オブジェクトに 'files'という属性がありません。 – HakariDo

+0

そこに構文エラーがあります。最後の行に 'shutil.move(os.path.join(root、f)、os.path.join(destinationpath、f))'と書いておきます。 –

+0

最終行を 'shutil.move(os.path.join(root、f)、os.path.join(destinationpath、f))'とします。エラーメッセージ: '\ Program \ Python \ Python36 \ lib \ shutil.py'および他の多くのエラー – HakariDo

関連する問題