일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 | 31 |
Tags
- Mac
- crud
- 배열
- 생활코딩
- ddit
- Error
- 자바문제
- servlet
- jsp
- 단축키
- html
- 맥
- pyqt
- Homebrew
- nodejs
- JDBC
- ibatis
- 자바
- 이클립스
- python
- spring
- 객체지향
- Android
- 대덕인재개발원
- FastAPI
- 반복문
- 컬렉션프레임워크
- Java
- Oracle
- API
Archives
- Today
- Total
romworld
pathlib 본문
python 3.4 버전부터 새로 도입된 파일 경로 및 디렉토리 경로를 다루는 라이브러리.
os.path 모듈보다 객체 지향적이고 간편한 방식으로 경로를 조작할 수 있음.
pathlib의 클래스인 Path 클래스는 파일이나 디렉토리의 경로를 나타내기 용이함.
from pathlib import Path
1. 파일 및 디렉토리 정보 추출
file_path = Path("/path/to/file.txt")
print(file_path.name) # 파일명: file.txt
print(file_path.stem) # 파일명 (확장자 제외): file
print(file_path.suffix) # 확장자: .txt
print(file_path.parent) # 부모 디렉토리: /path/to
2. 존재 여부 확인
existing_file = Path("/path/to/existing_file.txt")
non_existing_file = Path("/path/to/non_existing_file.txt")
print(existing_file.exists()) # True
print(non_existing_file.exists()) # False
3. 생성 및 삭제
new_file = Path("/path/to/new_file.txt")
new_file.touch() # 새로운 파일 생성
existing_file.unlink() # 기존 파일 삭제
4. 경로 조합
path1 = Path("/home/user")
path2 = path1 / "documents" / "file.txt"
'Python' 카테고리의 다른 글
Python 24 - FastAPI를 이용한 오목게임 (0) | 2023.01.12 |
---|---|
Python 22 - SQLite를 이용한 CRUD (mem 테이블) (0) | 2023.01.10 |
Python 21 - (JavaScript) 구구단, 홀짝,로또,가위바위보,별찍기, 전화번호 입력, 계산기, 야구게임 (0) | 2023.01.09 |
Python 20 - CRUD (emp 테이블) (0) | 2023.01.06 |
Python 19 - DAO 만들기 (0) | 2023.01.06 |
Comments