
dirname() 함수를 사용하여 디렉토리 이름을 추출하는 방법은 다음과 같습니다.
#hostingforum.kr
python
import os
path = '/Users/username/Documents/project'
dirname = os.path.dirname(path)
print(dirname) # '/Users/username/Documents'
위 코드에서 os.path.dirname() 함수를 사용하여 path 변수의 디렉토리 이름을 추출하고, 결과를 print() 함수를 사용하여 출력합니다.
또한, os.path.dirname() 함수는 경로의 디렉토리 이름을 추출할 때, 경로의 마지막 디렉토리 이름을 반환합니다.
예를 들어, '/Users/username/Documents/project' 경로의 디렉토리 이름은 '/Users/username/Documents'입니다.
이러한 기능을 사용하여 파일 경로에서 디렉토리 이름을 추출할 수 있습니다.
2025-06-22 12:49