# OS 모듈 os 모듈은 파이썬에서 운영 체제와 직접 상호 작용할 수 있도록 해주는 표준 라이브러리입니다. 파일, 디렉토리, 프로세스 등 운영 체제의 다양한 기능을 파이썬 코드에서 활용할 수 있도록 지원합니다. ## 파일 시스템 조작 파일 생성, 삭제, 이동, 복사, 이름 변경 등 * `os.rename(src, dst)`: 파일 또는 디렉토리의 이름을 변경합니다. * `os.remove(path)`, `os.unlink(path)`: 파일을 삭제합니다. * `os.link(src, dst)` ## 디렉토리 관리 디렉토리 생성, 삭제, 목록 조회 등 * `os.getcwd()`: 현재 작업 디렉토리의 경로를 반환합니다. * `os.chdir(path)`: 작업 디렉토리를 변경합니다. * `os.listdir(path)`, `os.walk(top[, topdown=True[, onerror=None[, followlinks=False]]])`, `os.path.walk(path, visit, arg)`: 지정된 디렉토리 내의 파일과 디렉토리 목록을 반환합니다. * `os.mkdir(path)`: 새로운 디렉토리를 생성합니다. * `os.makedirs(path)`: 중첩된 디렉토리를 생성합니다. * `os.rmdir(path)`, `os.removedirs(path)`, `shutil.rmtree(path)`: 빈 디렉토리를 삭제합니다. ## 파일 정보 파일 크기, 수정 시간, 권한 등을 조회 * `os.path.abspath(path)`, `os.path.realpath(path)` * `os.path.dirname(path)` * `os.path.exists(path)`: 파일 또는 디렉토리가 존재하는지 확인합니다. * `os.path.isfile(path)`: 주어진 경로가 파일인지 확인합니다. * `os.path.isdir(path)`: 주어진 경로가 디렉토리인지 확인합니다. * `os.chmod(path, mode)` * `os.chown(path, uid, gid)` * `os.path.getatime(path)`, ` os.path.getmtime(path)`, ` os.path.getctime(path)` * `os.path.getsize(path)` * `os.path.samefile(path1, path2)` ## 환경 변수 시스템 환경 변수에 접근 * `os.environ`: 환경 변수를 딕셔너리 형태로 제공합니다. ## 프로세스 관리 프로세스 실행, 종료 등 ```python import os # 현재 작업 디렉토리 출력 print(os.getcwd()) # 새로운 디렉토리 생성 os.mkdir("new_directory") # 파일 목록 출력 for file in os.listdir(): print(file) # 파일 복사 (shutil 모듈 활용) import shutil shutil.copy("source.txt", "destination.txt") ``` ## 주의할 점 * 파일 시스템 조작: 파일 시스템을 조작할 때는 주의해야 합니다. 잘못된 사용으로 데이터가 손실될 수 있습니다. * 경로 구분: 운영 체제에 따라 경로 구분자가 다릅니다. (윈도우: \, 리눅스/맥: /) * 권한: 파일 시스템에 접근하기 위한 충분한 권한이 필요합니다.