본문 바로가기
파이썬

파이썬 폴더의 모든 CSV 파일을 반복합니다.

by º기록 2021. 1. 28.
반응형

여러 종류의 파일과 많은 폴더가 포함 된 폴더의 csv 파일 만 반복하려고합니다.이 폴더에있는 모든 .csv 파일을 나열하려고합니다.

제가 의미하는 바는 다음과 같습니다.

import os, sys

path = "path/to/dir"
dirs = os.listdir(path)

for file in dirs:
    if file == '*.csv':
        print file

파이썬에는 와일드 카드 변수가 없다는 것을 알고 있지만 이것을 수행하는 방법이 있습니까?

 

해결 방법

 


>>> import glob
>>> glob.glob('/path/to/dir/*.csv')

경로 이름과 일치하는 경로 이름의 비어있을 수있는 목록을 반환합니다. must be a string containing a path specification. pathname can be either absolute (like /usr/src/Python-1.5/Makefile) or relative (like ../../Tools//.gif), and can contain shell-style wildcards. Broken 심볼릭 링크는 결과에 포함됩니다 (셸에서와 같이).

 

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

 

 

반응형

댓글