티스토리 뷰
Stack Overflow에 자주 검색, 등록되는 문제들과 제가 개발 중 찾아 본 문제들 중에서 나중에도 찾아 볼 것 같은 문제들을 정리하고 있습니다.
Stack Overflow에서 가장 먼저 확인하게 되는 가장 높은 점수를 받은 Solution과 현 시점에 도움이 될 수 있는 가장 최근에 업데이트(최소 점수 확보)된 Solution을 각각 정리하였습니다.
아래 word cloud를 통해 이번 포스팅의 주요 키워드를 미리 확인하세요.
Determining Whether a Directory is Writeable
디렉토리 쓰기 가능 여부 확인하기
문제 내용
What would be the best way in Python to determine whether a directory is writeable for the user executing the script? Since this will likely involve using the os module I should mention I'm running it under a *nix environment.
Python에서는 스크립트를 실행하는 사용자가 디렉토리를 쓸 수 있는지 여부를 결정하는 가장 좋은 방법이 무엇일까요? 이 작업은 아마도 os 모듈을 사용해야 할 것입니다. *nix 환경에서 실행 중임을 언급해야 합니다.
높은 점수를 받은 Solution
Although what Christophe suggested is a more Pythonic solution, the os module does have the os.access function to check access:
Christophe가 제안한 것이 더 파이썬답긴 하지만, os 모듈에는 액세스를 확인하는 os.access 함수가 있습니다.
os.access('/path/to/folder', os.W_OK)
# W_OK is for writing, R_OK for reading, etc.
os.access('/path/to/folder', os.W_OK) # 쓰기 가능은 W_OK, 읽기 가능은 R_OK 등입니다.
가장 최근 달린 Solution
if os.access(path_to_folder, os.W_OK) is not True:
print("Folder not writable")
else :
print("Folder writable")
more info about access can be find it here
액세스에 대한 자세한 정보는 여기에서 찾을 수 있습니다.
출처 : https://stackoverflow.com/questions/2113427/determining-whether-a-directory-is-writeable
'개발 > 파이썬' 카테고리의 다른 글
서브폴더를 모듈로 가져오기 (0) | 2023.02.08 |
---|---|
Python 3에서 generator에 type hint를 적용하기 (0) | 2023.02.08 |
하위 디렉토리를 탐색하지 않고 현재 디렉토리 내 파일들만 검색하기 (0) | 2023.02.07 |
딕셔너리에서 첫 번째 키를 찾는 방법 (0) | 2023.02.05 |
리스트 zip을 반대로 수행하기(튜플 리스트를 두개의 리스트로 변환하기) (0) | 2023.02.04 |