[python] 파일경로라이브러리 pathlib

ggydo59·2023년 3월 22일
0

💡 파이썬 파일관련 라이브러리인 pathlib의 사용법을 정리합니다.

from pathlib import Path

# 현재 디렉토리내의 파일/폴더 들을 리스트로 출
p = Path('.')
[x for x in p.iterdir() if x.is_dir()]
""" [PosixPath('.hg'), PosixPath('docs'), PosixPath('dist'),
 PosixPath('__pycache__'), PosixPath('build')] """

# 파일 경로 설정하는 방법
p = Path('/etc')
q = p / 'init.d' / 'reboot'
q
# PosixPath('/etc/init.d/reboot')
q.resolve() # 절대경로로 얻음
# PosixPath('/etc/rc.d/init.d/halt')

# quering path properties:
q.exists()
True
q.is_dir()
False

참고


profile
데이터엔지니어입니다.

0개의 댓글