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 中直接使用。