Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- 이미지빌드
- 좌표거리
- 도커
- GNN
- 그리드분할
- osmnx
- geojson
- 패치분할
- Set
- 지하철역좌표
- STL
- pyvista
- 폴더조사
- graph
- GCN
- 귀여운고래
- 알고리즘
- geopandas
- GIS
- 데이터입수
- Python
- MESH
- 컨테이너
- 3d
- 동명이인찾기
- 파이썬
- 3d데이터
- 도커 레이어
- python최단거리
- docker
Archives
- Today
- Total
이것저것 기록
[python] 폴더/하위 폴더의 파일 목록 출력 본문

1. 특정 형태의 파일 위치 출력
import os
import pandas as pd
import csv
PATH = r'C:/Users/82107/Desktop/COMPAS_세종시/'
file_ls = []
max_depth = 0
for path, dir, files in os.walk(PATH):
for file in files:
current = os.path.join(path, file).replace('\\', '/')
file_ls.append(current)
if len(current.split('/')) > max_depth:
max_depth = len(current.split('/'))
- PATH에 mother root을 입력
- 해당 코드는 pdf 파일의 위치만 추출하는 것인데, 만약에 csv 파일의 위치를 추출하고 싶다면 if 문을 수정하면 된다
2. 폴더와 하위폴더 내 모든 파일 목록을 확인하고 싶다면? (데이터 입수)
import os
import pandas as pd
import csv
PATH = r'C:/Users/82107/Desktop/COMPAS_세종시/'
file_ls = []
max_depth = 0
for path, dir, files in os.walk(PATH):
for file in files:
current = os.path.join(path, file).replace('\\', '/')
file_ls.append(current)
if len(current.split('/')) > max_depth:
max_depth = len(current.split('/'))
csv_path = 'C:/Users/82107/Desktop/'
os.chdir(csv_path)
depth = list(range(0, max_depth, 1))
with open('folder_data.csv', 'w', newline='', encoding='cp949') as f:
w = csv.writer(f)
w.writerow(' '.join(str(e) for e in depth).split())
for path, dir, files in os.walk(PATH):
for file in files:
current = os.path.join(path, file).replace('\\', '/')
row = os.path.relpath(current, PATH).split(os.sep)
w.writerow(row)
df = pd.read_csv('folder_data.csv', error_bad_lines=False, index_col=None,
header=0, engine='python', encoding='cp949')
- 폴더의 깊이만큼 열을 만들고 (depth)
- 파일 경로를 '/' 단위로 잘라서 행 값으로 입력해준다
- 이때 특정 형태의 파일만 조사하고 싶다면 1번과 같이 if 문을 추가해주면 된다
'코린이 > 실무를 위한 코딩 기록' 카테고리의 다른 글
[python] STL to VTK / VTK to STL (0) | 2021.12.13 |
---|---|
[python] 3D Mesh (.stl) 데이터 그리드(패치) 분할하기 (2) | 2021.07.01 |
[python, GIS] 서울시 지하철역 노선정보+좌표 데이터 구축하기 (3) | 2021.04.17 |
[python, plotly] Choropleth 지도로 위치/속성 지도 데이터 시각화 하기 (3) | 2021.04.12 |
[python, GIS] 도로명주소 데이터 좌표계 확인 및 변환 (16) | 2021.02.19 |
Comments