在Python中,你可以使用`os`模块中的`chdir`函数来切换当前工作目录。以下是具体的步骤和示例代码:
1. 导入`os`模块:
import os
2. 使用`os.chdir`函数切换到指定的目录:
new_directory = "/path/to/directory" 替换为你想切换到的目录路径
os.chdir(new_directory)
3. 打印当前工作目录以确认切换成功:
print(os.getcwd())
例如,如果你想切换到`D:\MachineLearning\kNN`目录,你可以这样写:
import os
new_directory = "D:\\MachineLearning\\kNN"
os.chdir(new_directory)
print(os.getcwd())
这样就可以在Python中实现命令行界面中的目录切换