import os path = os.getcwd() print(path) # Out: C:\Users\hakun print(type(path)) # Out: <class 'str'> os.chdir('.\デスクトップ') path = os.getcwd() print(path) # Out: C:\Users\hakun\デスクトップ # この場合以下のどっちも同じディレクトリに行く os.chdir('..') os.chdir(os.path.dirname(os.path.abspath('sample.py')))
Google Colaboratoryでのファイルの読み込み方
・これは絶対パスだ。/content/drive/MyDrive/sample.py
・パスを通すこと:
import sys root_path = 'drive/MyDrive/' sys.path.append(root_path) import sample # Out: a1 contains the value of 10