Notice
Recent Posts
Recent Comments
Link
SeouliteLab
[Python/파이썬] 현재 디렉토리 위치(Working Directory) 얻는 방법 본문
현재 디렉토리 위치를 얻는 것은 파일을 다룰 때 매우 유용한 기능입니다. 파이썬에서는 여러 가지 방법을 통해 현재 작업 디렉토리를 확인할 수 있습니다. 이번 글에서는 이를 위한 여러 가지 방법과 예제를 살펴보겠습니다.
1. os 모듈을 사용하는 방법
os 모듈을 사용하여 현재 디렉토리 위치를 얻을 수 있습니다. os.getcwd()
함수를 사용하면 현재 작업 디렉토리를 얻을 수 있습니다.
import os
# 현재 디렉토리 위치 얻기
current_directory = os.getcwd()
print("Current Directory:", current_directory)
2. pathlib 모듈을 사용하는 방법
pathlib 모듈은 파일 시스템 경로를 다루는 데 유용한 클래스를 제공합니다. Path.cwd()
메서드를 사용하여 현재 작업 디렉토리를 얻을 수 있습니다.
from pathlib import Path
# 현재 디렉토리 위치 얻기
current_directory = Path.cwd()
print("Current Directory:", current_directory)
예제 코드:
os 모듈을 사용하는 방법
import os
# 현재 디렉토리 위치 얻기
current_directory = os.getcwd()
print("Current Directory:", current_directory)
pathlib 모듈을 사용하는 방법
from pathlib import Path
# 현재 디렉토리 위치 얻기
current_directory = Path.cwd()
print("Current Directory:", current_directory)
'프로그래밍' 카테고리의 다른 글
[Python/파이썬] 동적 import와 함수 호출 방법 (0) | 2024.03.06 |
---|---|
[Python/파이썬] Python3에서 다른 경로에 있는 파일을 import 하는 방법 (0) | 2024.03.06 |
[Python/파이썬] ChromeDriver 버전 문제 - This version of ChromeDriver only supports Chrome version 76 (0) | 2024.03.06 |
[Python/파이썬] JSON 파일 읽고 쓰는 방법 (0) | 2024.03.06 |
[Python/파이썬] Google Translate으로 Markdown 문서 번역하기 (0) | 2024.03.06 |