2017-12-22 21 views
0

다음 코드는 각 하위 디렉토리의 이미지 개수를 계산합니다. 방법 하위 디렉토리에 이미지가 이상 2
N13은 주 디렉토리 =>300 하위 디렉토리 (1 ... 300) => 각각의 하위 디렉토리를 가지고있는 한 인 경우 하위 디렉토리를 삭제합니다 이미지.If 조건에 따라 현재 디렉토리 삭제

출력 :
이미지 : 2, 디렉토리 : 1 개
이미지 : 3, 디렉토리 : 2 개
이미지 : 4, 디렉토리 : 당신은 폴더를 삭제 shutil.rmtree()를 사용할 수있는 3

import os 
path='C:/n13/' 
def count_em(path): 
    x = 0 
    for root, dirs, files in os.walk(path): 
     files_count = (len(files)) 
     x = x + 1 
     print("Images:",files_count,"Directory:",x) 
    return files_count 
+0

경우 FILES_COUNT> 2 : shutil.rmtree (os.walk (경로)) – Shiva

+0

오류 "lstat는 : 경로 문자열이어야 os.PathLike 바이트가 아닌 발전기" –

+0

os.walk (경로)하여 이 장소는 서브 디렉토리 경로를 제공합니다 – Shiva

답변

0

하위 디렉토리 및 파일과 함께.

import os 
import shutil 

path='C:/n13/' 

def count_em(path): 
    x = 0 
    files_count = 0 
    for root, dirs, files in os.walk(path): 
     files_count = (len(files)) 
     if files_count >= 2: 
      shutil.rmtree(root) 
     x = x + 1 
     print("Images:", files_count, "Directory:", x) 
    return files_count 


count_em(path) 
+0

https://stackoverflow.com/questions/47964336/two-pass-connected-component-number-of-components-issue/47964562#47964562이 문제를 확인하십시오 –