Python/Python os
Python os : os.path.exists (directory 또는 file 존재 여부 확인)
CosmosProject
2021. 6. 30. 13:43
728x90
반응형
Syntax
os.path.exists(file_path)
os.path.exists는 위처럼 사용 가능합니다.
parameter로서 file_path를 받으며
file_path가 실제로 존재하면 True를
존재하지 않으면 False를 반환합니다.
|-- test.py
|-- test
|-- sub_test
|-- test.csv
# test.py
import os
print(os.path.exists('test/')) # 1
print(os.path.exists('test/empty_test/')) # 2
print(os.path.exists('test/sub_test/test.csv')) # 3
-- Result
True
False
True
위 예시를 봅시다.
python code는 test.py에 적혀있습니다.
그리고 test/sub_test/test.csv라는 directory가 존재하죠.
1. test.py와 동일한 위치에 test라는 folder가 존재하므로 True를 반환합니다.
2. test.py 기준 상대경로로 따졌을 때 test/empty_test/ 라는 directory는 존재하지 않습니다. False를 반환합니다.
3. test.py 기준 상대경로로 따졌을 때 test/sub_test/test.csv 라는 directory와 파일이 존재합니다. True를 반환합니다.
(이렇게 폴더 뿐 아니라 파일의 유무도 체크할 수 있습니다.)
728x90
반응형