在Python中,可以使用`os`模块来切换本地目录。以下是使用`os.chdir()`函数切换目录的基本方法:
import os切换到指定目录os.chdir('/path/to/directory')打印当前工作目录以确认切换成功print(os.getcwd())
如果你需要创建一个不存在的目录,然后切换到该目录,可以使用`os.path.join()`和`os.makedirs()`函数:

import os创建目录路径path = os.path.join('/tmp', 'new_directory')检查目录是否存在if not os.path.exists(path):创建目录os.makedirs(path)切换到目录os.chdir(path)打印当前工作目录以确认切换成功print(os.getcwd())
请注意,路径可以是绝对路径或相对路径。使用`os.path.abspath()`和`os.path.dirname(__file__)`可以获取当前文件所在目录的绝对路径。
