반응형
os.path.dirname (__ file__)이 비어 있음을 반환
.py 파일이 실행되는 현재 디렉토리의 경로를 얻고 싶습니다.
예를 들어 D:\test.py
코드가 있는 간단한 파일 :
import os
print os.getcwd()
print os.path.basename(__file__)
print os.path.abspath(__file__)
print os.path.dirname(__file__)
출력이 다음과 같은 것이 이상합니다.
D:\
test.py
D:\test.py
EMPTY
와에서 동일한 결과를 기대 getcwd()
하고 path.dirname()
있습니다.
주어진 os.path.abspath = os.path.dirname + os.path.basename
이유
os.path.dirname(__file__)
비어있는?
os.path.abspath = os.path.dirname + os.path.basename
보유하지 않기 때문에 . 우리는 오히려
os.path.dirname(filename) + os.path.basename(filename) == filename
모두 dirname()
와 basename()
전용 계정에 현재 디렉토리를 복용하지 않고 구성 요소로 전달 된 파일 이름을 분할합니다. 현재 디렉토리도 고려하려면 명시 적으로 지정해야합니다.
절대 경로의 dirname을 얻으려면
os.path.dirname(os.path.abspath(__file__))
그런 식으로도 사용할 수 있습니다 :
dirname(dirname(abspath(__file__)))
print(os.path.join(os.path.dirname(__file__)))
이 방법으로도 사용할 수 있습니다
os.path.split(os.path.realpath(__file__))[0]
os.path.realpath(__file__)
현재 스크립트의 절대 값을 반환합니다. os.path.split (abspath) [0]은 현재 디렉토리를 반환
import os.path
dirname = os.path.dirname(__file__) or '.'
참고 URL : https://stackoverflow.com/questions/7783308/os-path-dirname-file-returns-empty
반응형
'Programming' 카테고리의 다른 글
.bat 파일을 사용하여 폴더가 있는지 확인 (0) | 2020.06.16 |
---|---|
ggplot을 사용하여 플롯을 만들 때 hjust와 vjust는 무엇을합니까? (0) | 2020.06.16 |
Android Studio를 다운로드하지 않고 Android SDK를 다운로드하려면 어떻게합니까? (0) | 2020.06.16 |
현재 페이지의 JavaScript 하드 새로 고침 (0) | 2020.06.16 |
외래 키는 쿼리 성능을 향상 시킵니까? (0) | 2020.06.16 |