[Python] 파일/폴더 경로 존재 유무 확인하는 함수
[Python] 파일/폴더 경로 존재 유무 확인하는 함수
구문
os.path.exists(경로)
설명
이 함수는 경로가 존재하면 True, 존재하지 않으면 False를 반환합니다.
예시
import os.path
if os.path.exists(r"C:\test1\test2.txt") :
print("파일 존재")
else :
print("파일 없음")
* python에서 string앞에 r을 붙여주면, string literal을 raw string으로 출력, 즉, 모든 Escape 문자를 그대로 출력해 줍니다.
참조
파이썬 도큐먼트
https://docs.python.org/3.11/library/os.path.html?highlight=path%20exists#os.path.exists
os.path — Common pathname manipulations
Source code: Lib/posixpath.py(for POSIX) and Lib/ntpath.py(for Windows). This module implements some useful functions on pathnames. To read or write files see open(), and for accessing the filesyst...
docs.python.org
os.path.exists(path)
Return True if path refers to an existing path or an open file descriptor. Returns False for broken symbolic links. On some platforms, this function may return False if permission is not granted to execute os.stat() on the requested file, even if the path physically exists.
Changed in version 3.3: path can now be an integer: True is returned if it is an open file descriptor, False otherwise.
Changed in version 3.6: Accepts a path-like object.