2023年12月5日火曜日

Python: 現在のディレクトリ取得と変更

現在のディレクトリを取得

import os

cwd = os.getcwd()

print(cwd)  # /Users/demo-user/documents/mydir


ディレクトリを変更

os.chdir('/Library')

cwd = os.getcwd()

print(cwd)  # /Library