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 中直接使用。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读:python魔法命令能做什么