- 증상
# Python에서 파일 오픈 시 권한 오류(Permission denied)가 발생하는 대부분의 경우는 다음과 같다
'''
1. 실제로 파일을 Read할 수 있는 권한이 없을 때
2. 파일이 아니라 폴더를 지정 했을 때
3. 파일이 없을 때
'''
- 해결
# 위의 오류는 대부분 파일이 아닌, 폴더를 직접 지정하는 경우에 발생하므로, 경로를 다시 확인해보면 대부분 해결 된다.
# 1. 만약, 폴더 하위에 있는 파일의 경로를 쉽게 모두 알아내고 싶다면 imutils 패키지를 사용해보자.
from imutils import paths
files = list(paths.list_files(pathFileRoot))
print(files)
'''
['pathImageRoot/a/image.jpg', 'pathImageRoot/b/text.txt', 'pathImageRoot/c/image.jpg', ...]
'''
# 2. imutils를 통해 원하는 확장자의 파일만 검색
exts = (".jpg", ".jpeg", ".png", ".bmp", ".tif", ".tiff")
files = list(paths.list_files(pathFileRoot, validExts=exts))
print(files)
'''
['pathImageRoot/a/image.jpg', 'pathImageRoot/c/image.jpg', ...]
'''
'프로그래밍 관련 > [Python] 간단 해결' 카테고리의 다른 글
[Tensorflow] Failed to load the native TensorFlow runtime. (0) | 2020.06.13 |
---|---|
[Python] glob 정규식으로 폴더 및 파일 제외 (0) | 2020.05.22 |
[Python] UnicodeDecodeError: 'cp949' codec can't decode byte 0xe2 in position 17533: illegal multibyte sequence (0) | 2020.05.21 |
[Python] cv2.imread 한글 경로 인식 문제 (0) | 2020.05.21 |
[Python] json.dumps() 한글 > 유니코드로 저장될 때 (0) | 2020.04.22 |