코랩 기초

boooookreeeed·2024년 10월 27일

google colab에서 현재 마운트된 경로를 확인하고
os 모듈을 사용해 그 경로를 변경하고 싶다

1) 현재 마운트된 경로 확인

from google.colab import drive
import os

# Google Drive 마운트
drive.mount('/content/drive')

# 현재 디렉터리 확인
current_dir = os.getcwd()
print(f"현재 경로: {current_dir}")

# Google Drive 마운트된 경로 설정
drive_root = '/content/drive/MyDrive'

# 마운트된 경로가 존재하는지 확인 후 경로 변경
if os.path.exists(drive_root):
    os.chdir(drive_root)
    print(f"경로가 {drive_root}로 변경되었습니다.")
else:
    print("Google Drive 경로를 찾을 수 없습니다.")

# 변경된 경로 확인
print(f"현재 작업 경로: {os.getcwd()}")

2) 경로 변경

# 경로를 Google Drive로 변경
os.chdir(drive_root)

# 변경된 경로 출력
new_dir = os.getcwd()
print(f"변경된 경로: {new_dir}")

주의사항

  • mount를 먼저 해야 한다.
  • drive가 올바르게 마운트되었는지 확인하려면 os.path.exists() 로 확인 가능
  • import 하면 다른 셀에서도 적용된다.
profile
you can do

0개의 댓글