Python 中沒有內置的 cd 命令,因為 cd 是 Unix 和 Linux 系統中的 shell 內置命令,用于更改當前工作目錄
import os
# 切換到指定目錄
os.chdir('/path/to/directory')
# 獲取當前工作目錄
current_directory = os.getcwd()
print(current_directory)
在這個示例中,我們使用 os 模塊中的 chdir() 函數來更改當前工作目錄,然后使用 getcwd() 函數獲取當前工作目錄。請注意,這種方法僅適用于 Python 腳本和程序,而不能在交互式 Python shell 中直接使用。