How to change the working directory in Python?
To change the working directory in Python, you can use the chdir() function in the os module. Here are the steps to change the working directory:
import os
# 获取当前工作目录
current_dir = os.getcwd()
print("当前工作目录:", current_dir)
# 更改工作目录为指定路径
new_dir = "C:/Users/username/Desktop"
os.chdir(new_dir)
# 获取更改后的工作目录
updated_dir = os.getcwd()
print("更新后的工作目录:", updated_dir)
After running the above code, the program will output the current working directory, the updated working directory, and the path you specified.