1 답변
-
1. glob.glob(pathname) 사용
glob.glob(pathname)
은pathname
과 대응하는 파일의 리스트를 return 해 줍니다.import glob import os #디렉토리를 바꿔야 할 경우에만 쓰세요 os.chdir("/mydir") #디렉토리를 바꿔야 할 경우에만 쓰세요 for file in glob.glob("*.txt"): #'*'은 모든 값을 의미 print(file)
2. os.listdir(path) 사용
os.listdir(path)
는 path로 주어진 디렉토리 안에 있는 이름의 리스트를 return해줍니다.import os for file in os.listdir("/mydir"): if file.endswith(".txt"): #끝이 ".txt"로 끝나는 경우 print(file)
댓글 입력