2011-07-09 8 views
0

私は非常に大きなファイルシステムで作業しています。私の仕事は、いくつかのパラメータでシステムを清掃することです。以下のプログラムの断片は、アイデアを与えることができます。ファイルのクリーニングプロセスに問題がありますか?

import DirectoryWalker 


extentions_to_delete = list([".rar",".doc",".URL",".js",".EXE",".mht",".css",".txt", ".cache", ".xml"]) 
extentions_to_copy = list([".jpg",".BMP",".GIF",".jpeg",".gif",".bmp",".png",".JPG"]) 

dw = DirectoryWalker.DirectoryWalker("/media/08247451247443AA/home/crap/") 

def copy_advice(key, files): 
    for ext in extentions_to_copy: 
     if(ext == key): 
      print str(len(files)) + " Files of type " + key + " should be coppied to the target folder." 
      for file in files: 
       copy_to = "/media/08247451247443AA/home/crap-pics/" 

       moved = dw.move_file_to(file, copy_to, True) 
       if not moved: 
        print file + " : not moved" 

walks = dw.get_all_file_types() 


for key in DirectoryWalker.Walk.store.keys(): 
    files = DirectoryWalker.Walk.store[key] 

    copy_advice(key, files) 

DirectoryWalkerに次のコードが書かれています。ウォークはstoreオブジェクトを持つシンプルなクラスです。

def get_all_file_types(self): 

    extentions = [] 

    for dirpath,dirnames,filenames in os.walk(self.dir_name): 
     for file in filenames: 
      extentions.append(Walk(dirpath +"/"+ file)) 

    return extentions    

def move_file_to(self, file_path, copy_to, rename_if_exists= False): 
     file_name = os.path.split(file_path)[1] 

     target_file_name = copy_to + file_name; 

     coppied = False 

     if not os.path.isfile(target_file_name): 
      coppied = True 
      try: 
       os.rename(file_path, target_file_name) 
      except OSError: 
       coppied = False 
       print "Oops! Unable to rename : " + file_path + " to target : " + target_file_name 

     if rename_if_exists: 
      coppied = True 
      file_name = "new_"+ file_name 
      try: 
       os.rename(file_path, target_file_name) 
      except OSError: 
       coppied = False 
       print "Oops! Unable to rename : " + file_path + " to target : " + target_file_name   

     return coppied 

Walkクラス

class Walk: 

    store = dict([]) 

    def __init__(self, filename): 

     self.file_ext = os.path.splitext(filename)[-1] 
     self.file_name = filename 

     if not (Walk.store.has_key(self.file_ext)): 
      Walk.store[self.file_ext] = list() 

     Walk.store[self.file_ext].append(self.file_name) 

しかし、プログラムが実行されると、それだけで、ほぼ10400ファイルを移動します。しかし、手動による計算では、ファイルシステムに13400個のファイルがあるはずです。私が間違っていることを教えてください。

アップデートソリューション慎重に調査した後

、私はそこに多くのあいまいなファイル名は、ターゲット・ファイル・システムにあり、それらのファイルが欠落していたという結果に出てきます。

答えて

1

とウォーククラスを置き換えることができ、私は、ターゲット・ファイル・システムとそれらのファイルには多くのあいまいなファイル名が存在し、その結果出てきます欠けていた

2

質問に答えるには、より簡単なコードをテストしてみましょう。サイドノートとして

import os 

all_files = [] 

for root, dirs, files in os.walk('/media/08247451247443AA/home/crap/'): 
    all_files.extend(files) 

print len(all_files) 

は、あなたが慎重に調査した後 defaultdict?

関連する問題