2011-07-09 57 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?