2016-04-10 22 views
0

Tüm dizini kaldıran bir komut dosyası var, ancak iki dosya (kodi.log ve kodi.old.log) dışında her şeyi silmek için bunu değiştirmek istiyorum, bu nedenle uzantının .log olması gerekir atlandı.İki dizinin dışında bir dizindeki tüm dosyaları kaldırın

Ben komut Herhangi bir fikir büyük takdir

TEMP = xbmc.translatePath(
    'special://home/temp' 
) 
folder = TEMP 
if os.path.exists(TEMP): 
    for the_file in os.listdir(folder): 
     file_path = os.path.join(folder, the_file) 
     try: 
      if os.path.isfile(file_path): 
       os.unlink(file_path) 
      elif os.path.isdir(file_path): shutil.rmtree(file_path) 
       donevalue = '1' 
     except Exception, e: 
      print e 

olduğunu.

cevap

0

Özyinelemeli aramalar kullanmayı deneyebilirsiniz. Bunu test edemedim ama aşağıdakiler işe yarar:

TEMP = xbmc.translatePath(
    'special://home/temp' 
) 
folder = TEMP 
def remove_directory(folder): 
    if os.path.exists(folder): 
     for the_file in os.listdir(folder): 
      file_path = os.path.join(folder, the_file) 
      if file_path in ("kodi.log", "kodi.old.log"): 
       continue 
      try: 
       if os.path.isfile(file_path): 
        os.unlink(file_path) 
       elif os.path.isdir(file_path): 
        remove_directory(file_path) 
        donevalue = '1' 
      except Exception, e: 
       print e 

Umarım yardımcı olur!

1

senin deyimi istediğiniz dosyanın adı bu takdirde anlamı the_file

if the_file not in ['kodi.log', 'kodi.old.log']: 
    # delete the file 

kontrol filepath

if os.path.isfile(file_path) and 'kodi.log' not in file_path and 'kodi.old.log' not in file_path: 
    # delete the file 

veya daha kompakt bir yol değilse kontrol etmek böyle olmalı eğer dosya kodi.log veya kodi.old.log değil, daha sonra sil

İlgili konular