[python] 디렉토리, 파일 존재 유무 확인하는 법
디렉토리, 파일 존재 여부 확인
isfile (파일 존재 여부 확인)
isfile은 파일의 경로를 넣어주면 되고 결과가 존재할 경우 True, 존재하지 않을 경우 False를 return 합니다.path로 symbolic link도 사용가능합니다.
symbolic link는 window의 바로가기(.lnk)라고 생각하시면 됩니다.
import os
file_path = "/Users/idaeung/programs/toeic_word/22.xlsx"
dir_path = "/Users/idaeung/programs/toeic_word"
print("isfile using file_path = {0}".format(os.path.isfile(file_path)))
print("isfile using dir_path = {0}\n".format(os.path.isfile(dir_path)))
# 출력
# isfile using file_path = True
# isfile using dir_path = False
file_path = "/Users/idaeung/programs/toeic_word/22.xlsx"
dir_path = "/Users/idaeung/programs/toeic_word"
print("isfile using file_path = {0}".format(os.path.isfile(file_path)))
print("isfile using dir_path = {0}\n".format(os.path.isfile(dir_path)))
# 출력
# isfile using file_path = True
# isfile using dir_path = False
file_path에는 file이 들어있는 경로를, dir_path에는 디렉토리의 경로가 각각 설정되어 있습니다.
isfile()에 각각 file_path와 dir_path를 넣은 결과 file이 있는지 없는지를 기준으로 True와 False를 반환합니다.
isdir (디렉토리 존재 여부 확인)
isdir은 디렉토리의 경로를 넣어주면 되고 결과가 존재할 경우 True, 존재하지 않을 경우 False를 return 합니다.path로 symbolic link도 사용가능합니다.
import os
file_path = "/Users/idaeung/programs/toeic_word/22.xlsx"
dir_path = "/Users/idaeung/programs/toeic_word"
print("isdir using file_path = {0}".format(os.path.isdir(file_path)))
print("isdir using dir_path = {0}".format(os.path.isdir(dir_path)))
# 출력
# isdir using file_path = True
# isdir using dir_path = False
file_path에는 file이 들어있는 경로를, dir_path에는 디렉토리의 경로가 각각 설정되어 있습니다.file_path = "/Users/idaeung/programs/toeic_word/22.xlsx"
dir_path = "/Users/idaeung/programs/toeic_word"
print("isdir using file_path = {0}".format(os.path.isdir(file_path)))
print("isdir using dir_path = {0}".format(os.path.isdir(dir_path)))
# 출력
# isdir using file_path = True
# isdir using dir_path = False
isdir()에 각각 file_path와 dir_path를 넣은 결과 dir이 있는지 없는지를 기준으로 True와 False를 반환합니다.
댓글
댓글 쓰기