2013-07-24 2 views
0

출력에 파일 및 폴더 구조를 재귀 적으로 표시하려고합니다.디렉토리에있는 파일을 재귀 적으로 나열 - python

실제 구조 :

Root--| 
     | 
     DIRA--| 
      | 
      DIRC--File5 
      File3 
      File4 
     File1 
     File2 
     DIRB--| 
      | 
      No File 

예상 출력 : 나는 아래의 다음과 같은 코드를 작성했습니다

Root: 
File1 
File2 

Root/DIRA 
File3 
File4 

Root/DIRA/DIRC 
File5 


Root/DIRB 
No File Found 

. 그러나 필요한 출력을 얻으려면 수정하는 방법과 마찬가지로 입력이 필요합니다.

코드

import os.path 

path = 'C:\\My\\path\\here' 

for root, dirnames, filenames in os.walk(path): 
    for subdirname in dirnames: 
     print subdirname 

    for filename in filenames: 
     print os.path.join(root, filename) 

실제 출력

DIRA 
DIRB 
C:\My\path\here\File1 
C:\My\path\here\File2 
DIRC 
C:\My\path\here\DIRA\File3 
C:\My\path\here\DIRA\File4 
C:\My\path\here\DIRA\DIRC\File5 
+0

들여 – Jiminion

답변

0
import os 

path = 'Root' 
for root, dirnames, filenames in os.walk(path): 
    print root 
    for filename in filenames: 
     print filename 
    if not filenames: 
     print 'No File Found' 
    print 
+0

감사 동료의 마지막. 사소한 수정을했고 기대했던 결과를 얻었다. – misguided

관련 문제