2010-02-10 3 views
1

몇개의 디렉토리가 있습니다. 7 일 이상 된 디렉토리를 지우고 싶습니다. 이미 구현 된 코드가 있지만 작동하지 않는 것 같습니다. 누구든지 내가 잘못 가고있는 것을 볼 수 있습니까? 어떤 도움파이썬의 오래된 디렉토리를 삭제하십시오.

폴더 샌드 박스 드롭하지만 삭제하지 마십시오에 대한

def delete_sandbox(): 

    for directories in os.listdir(os.getcwd()): 

     if not os.path.isdir(directories) or not os.stat(directories).st_ctime < time.time()-(7*24*3600): 
      continue 
     os.chdir(directories) 
     drop_sandbox() 
     os.chdir(rootDir) 
     os.system("sudo rm -rf "+directories) 
     print 'Folders older than 7 days old dropped and removed' 

감사합니다. 프로그램이이 폴더들 각각으로 들어가고, 샌드 박스를 떨어 뜨리고, 루트 디렉토리에 chnage를 다시 보내고, 모든 오래된 디렉토리들을 삭제하기를 원합니다. 이 작업을 수행 할 때 폴더가 여전히 존재합니다.

또한 폴더 이름에 저장된 문자열 날짜로 디렉터리를 삭제할 때도이 기능이 작동했습니다. 하지만 이제는 작동을 멈춘 타임 스탬프를 얻으려고합니다.

나는 'rm -rf'+directories을 테스트했으며 이전 폴더는 삭제하지 않습니다.

Traceback (most recent call last): 
    File "yep.py", line 21, in <module> 
    delete_sandbox() 
    File "yep.py", line 18, in delete_sandbox 
    shutil.rmtree(directories) 
    File "/home/build/workspace/downloads/Python-2.6.4/Lib/shutil.py", line 208, in rmtree 
    onerror(os.listdir, path, sys.exc_info()) 
    File "/home/build/workspace/downloads/Python-2.6.4/Lib/shutil.py", line 206, in rmtree 
    names = os.listdir(path) 
OSError: [Errno 2] No such file or directory: 'Debug' 

이 폴더를 삭제할 수있는 다른 방법이 있나요 : 나는 shutil.rmtree을하려고 할 때 오류 메시지가?

나는 그것을 작동 시켰고, 나는 shutil.rmtree을 사용했고 모두 작동하는 것처럼 보였다. 어떤 도움을 주셔서 감사합니다. 수정 된 코드는 다음과 같습니다.

def delete_sandbox(): 

    for directories in os.listdir(os.getcwd()): 

     if not os.path.isdir(directories) or not os.stat(directories).st_ctime < time.time()-(sbox_age): 
      continue 
     os.chdir(directories) 
     drop_sandbox() 
     os.chdir(rootDir) 
     shutil.rmtree(directories) 
     print 'Sandboxes older than 7 days old dropped and removed' 

delete_sandbox() 
+3

그것이 * 무슨 뜻 *? 오류 메시지가 나타 납니까? – SilentGhost

+0

일부 디렉토리 이름이 공백 (os.system 호출시)을 포함하거나 입력시 현재 디렉토리가 'rootDir'이 아닌 경우이 작업은 실패합니다. 오류가 발생 했습니까? 그렇다면 어디에서 무엇이 있습니까? 예기치 않은 행동, 그렇다면 무엇을 어디에서? 일부'print's는 우리에게 더 많은 정보를 줄 것입니다 (현재 우리는 0 비트 정도를줍니다 ;-). –

+0

시스템에 전화 할 때, 특히 'rm'이 관련되었을 때 일반적인 제안/경고. 기능을 실행하려면 기본적으로 True로 실행되는 시스템 명령을 인쇄하는 dry-run 옵션을 지정하십시오. – MattH

답변

9
import os 
import time 
import shutil 
numdays = 86400*7 
now = time.time() 
directory=os.path.join("/home","path") 
for r,d,f in os.walk(directory): 
    for dir in d: 
     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" 
+0

+1의 경우. os.system ("rm -rf") –

1

os.listdir은 상대 경로 인 문자열 목록을 반환합니다. rootdir이 chdir 일 때, rootDir이 무엇인지에 따라이 경로는 더 이상 유효하지 않을 수 있습니다.

1
  • drop_sandbox()의 기능은 무엇입니까? (당신이 우리에게 준 함수는 delete_sandbox()입니다.) 아마 이것은 재귀 함수가 될 수 있으며 잘못된 함수 이름을 사용했을 수도 있습니다.
  • rootDir은 전역 변수입니까? 아마도 당신이 의미 한 것입니까 os.chdir("..")
  • rootDir에는 무엇이 있습니까? os.listdir은 상대 경로를 제공합니다. rootDir이 검색 기준이되면 나열한 디렉토리가 작동하지 않을 수 있습니다. 더 심하다 : 만약 그렇다면, 당신은 여전히 ​​원하는 것을 지울 수 있습니다.

또한 shutil 패키지에는 살펴볼 기능이 rmtree 있습니다.

0

허용되는 대답의 일반적인 개념은 올바르지 만 구현에는 그렇지 않은 몇 가지 점이 있습니다.

  1. 가변 이름은 문자로 청구되지 않습니다.

    class CompRM(object): 
        exception_list = [] 
        path = None 
        comparator = None 
        comparator_target = None 
        comparator_delete_condition = None 
    
        delete_directories = False 
        delete_files = False 
    
        def __init__(self, target_path, comparator, comparator_target='st_mtime', comparator_delete_condition='>', delete_directories=False, delete_files=False): 
         self.path = target_path 
         self.comparator = comparator 
         self.comparator_target = comparator_target 
         self.comparator_delete_condition = comparator_delete_condition 
    
         for directory_path, directory_names, file_names in os.walk(self.path): 
          if delete_directories is True: 
           self.walk_names(directory_path, directory_names) 
          if delete_files is True: 
           self.walk_names(directory_path, file_names) 
    
         if len(self.exception_list) > 0: 
          raise Exception({'message': 'Encountered exceptions while deleting.', 'debug': self.exception_list}) 
    
        def walk_names(self, directory_path, names): 
         for _name in names: 
          _path = os.path.join(directory_path, _name) 
          _stat = os.stat(_path) 
          _value = getattr(_stat, self.comparator_target) 
          if self.delete_condition_met(_value): 
           self.safe_remove(_path) 
    
        def delete_condition_met(self, value): 
         if self.comparator > value and self.comparator_delete_condition is '>': 
          return True 
         if self.comparator < value and self.comparator_delete_condition is '<': 
          return True 
         return False 
    
        def safe_remove(self, remove_path): 
         print 'remove', remove_path 
         shutil.rmtree(remove_path, onerror=self.register_exception) 
    
        def register_exception(self, function, path, exception_info): 
         self.exception_list.append({ 
          function: function, 
          path: path, 
          exception_info: exception_info, 
         }) 
    

    당신은 다음과 같이 호출 할 수 있습니다 :

    내가 아이디어를했다 조금 더 많은 기능을 필요로하고이 내장 된 이후
  2. 는 디렉토리와 같은 파이썬 방법()

을 덮어 쓰지 마십시오

comprm = CompRM('/tmp', time.time() - 60*60*24*7, comparator_target='st_ctime', delete_files=True) 

여기서 comparator_target은 비교를 위해 취한 os.stat()의 attributename입니다.

감사합니다, 유스

작동하지 않는 것
+0

클래스 수준에서'exception_list = []'이 (가) 버그입니다. 이 목록은 인스턴스간에 공유됩니다. 가변 속성은 항상 그것을 피하기 위해'__init__'에서 설정 될 필요가 있습니다 (일반적으로 인스턴스에서 설정/사용하는 모든 속성에 대해 이것을 수행하고 실제로 액세스하려고 할 때만 클래스 레벨 속성을 사용하는 것이 좋습니다 클래스 레벨에서) – ThiefMaster

+0

또한 'is'는 비교에 잘못되었습니다. 이를 위해'=='를 사용하십시오 - 동일한 객체를 검사하는 것이고 그것은 순수한 우연과 특정 짧은 문자열에 대해 작동하는 구현 세부 사항입니다. – ThiefMaster

관련 문제