본문 바로가기
파이썬

파이썬 디렉토리에있는 파일 만 가져 오는 방법은 무엇입니까?

by º기록 2020. 12. 24.
반응형

이 코드가 있습니다.

allFiles = os.listdir(myPath)
for module in allFiles:
    if 'Module' in module: #if the word module is in the filename
        dirToScreens = os.path.join(myPath, module)    
        allSreens = os.listdir(dirToScreens)

이제 모든 것이 잘 작동합니다. 선을 변경하면됩니다.

allSreens = os.listdir(dirToScreens)

폴더가 아닌 파일 목록을 가져옵니다. 따라서 내가 사용할 때

allScreens  [ f for f in os.listdir(dirToScreens) if os.isfile(join(dirToScreens, f)) ]

그것은 말한다

module object has no attribute isfile

참고 : Python 2.7 을 사용하고 있습니다.

 

해결 방법

 


import os
from os import path
files = [f for f in os.listdir(dirToScreens) if path.isfile(f)]

또는 기능적으로 느끼면 : D

files = filter(path.isfile, os.listdir(dirToScreens))

 

참조 페이지 https://stackoverflow.com/questions/21384232

 

 

반응형

댓글