파일 디렉토리는 파일과 폴더(디렉토리)들의 계층 구조를 나타내는 방법입니다. 파일 디렉토리를 사용하여 파일을 쉽게 찾고 정리할 수 있습니다. Python에서는 기본 파일 디렉토리를 다루기 위한 다양한 내장 모듈을 제공합니다.
os 모듈은 파일 시스템과 상호 작용하기 위한 기능을 제공합니다. 다음은 os 모듈을 사용하여 파일 디렉토리 작업을 수행하는 예제 코드입니다:
```python
import os
# 현재 작업 디렉토리 출력
print("Current working directory:", os.getcwd())
# 디렉토리 생성
os.mkdir("example_directory")
# 디렉토리 이름 변경
os.rename("example_directory", "new_directory")
# 디렉토리 내용 확인
print("Contents of the directory:", os.listdir("new_directory"))
# 디렉토리 삭제
os.rmdir("new_directory")
```
위의 코드에서는 `os.getcwd()` 함수를 사용하여 현재 작업 디렉토리를 확인합니다. `os.mkdir()` 함수를 사용하여 "example_directory"라는 이름의 디렉토리를 생성하고, `os.rename()` 함수를 사용하여 디렉토리의 이름을 "new_directory"로 변경합니다. `os.listdir()` 함수를 사용하여 디렉토리의 내용을 확인하고, `os.rmdir()` 함수를 사용하여 디렉토리를 삭제합니다.
Python에서 파일 디렉토리 작업을 수행하기 위해 더 자세한 내용은 다음 Python 문서를 참조하세요:
https://docs.python.org/ko/3/library/os.html
댓글