Change Directory in Python: Quick Guide
To switch to a specific directory, you can utilize the chdir() function in the os module of Python. Here is an example code demonstrating how to change to the desired file directory:
import os
# 指定要切换到的文件目录
new_directory = '/path/to/directory'
# 切换到指定目录
os.chdir(new_directory)
# 打印当前工作目录,以确认是否成功切换
print(os.getcwd())
In the code above, a new directory is defined by specifying a path first, then the current working directory is changed to the new directory using the os.chdir() function. Finally, the os.getcwd() function is used to print the current working directory to confirm if the switch to the specified file directory was successful.