2011-05-26 65 views
0

這是功能我正在使用Python來刪除舊迪爾斯需要使用Python幫助刪除舊迪爾斯腳本

def delete_olddirs(days,file_path): 
    numdays = 60*60*24*days 
    now = time.time() 

    for dir in os.listdir(file_path): 
       r = file_path 
       timestamp = os.path.getmtime(os.path.join(r,dir)) 
       if now-numdays > timestamp: 
        try: 
         print "removing ",os.path.join(r,dir) 
         #shutil.rmtree(os.path.join(r,dir)) #uncomment to use 
        except Exception,e: 
         print e 
         pass 
        else: 
         print "some message for success" 

的問題是,每次我看到消息removing . ....我也看到消息

some message for success 

我想知道爲什麼每次都執行部件

回答

0

如果沒有引發異常時執行的tryelse部分。

0

您正在使用try/except/else塊。如果try塊成功(except除外),那麼else塊將被執行。這是正常的行爲。

0

試試這個

l=[] 
    for dir in os.listdir(file_path): 
     r = file_path 
     timestamp = os.path.getmtime(os.path.join(r,dir)) 
     if now-numdays > timestamp: 
      try: 
       print "removing ",os.path.join(r,dir) 
       #shutil.rmtree(os.path.join(r,dir)) #uncomment to use 
       l.append("%s" %os.path.join(r,dir)) 
      except Exception,e: 
       print e 
    print "removed the following folders %s" %L