몇개의 디렉토리가 있습니다. 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()
그것이 * 무슨 뜻 *? 오류 메시지가 나타 납니까? – SilentGhost
일부 디렉토리 이름이 공백 (os.system 호출시)을 포함하거나 입력시 현재 디렉토리가 'rootDir'이 아닌 경우이 작업은 실패합니다. 오류가 발생 했습니까? 그렇다면 어디에서 무엇이 있습니까? 예기치 않은 행동, 그렇다면 무엇을 어디에서? 일부'print's는 우리에게 더 많은 정보를 줄 것입니다 (현재 우리는 0 비트 정도를줍니다 ;-). –
시스템에 전화 할 때, 특히 'rm'이 관련되었을 때 일반적인 제안/경고. 기능을 실행하려면 기본적으로 True로 실행되는 시스템 명령을 인쇄하는 dry-run 옵션을 지정하십시오. – MattH