■ exists 함수를 사용해 디렉토리/파일 경로의 존재 여부를 구하는 방법을 보여준다.
▶ 예제 코드 (PY)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import os.path print(os.path.exists("C:\\Program Files\\Python310")) print(os.path.exists("C:\\Program Files\\Python310\\python.exe")) print(os.path.exists("C:\\Program Files\\Python310\\temp")) print(os.path.exists("C:\\Program Files\\Python310\\temp\\test.txt")) """ True True False False """ |
※ 리눅스와 같은 OS에서는 파일이나 디렉토리가 존재하지만 읽기 권한이 없는 경우에도 False를 반환할 수 있다.