如何在 Python 中设置当前工作目录?
您可以使用模块在 Python 中设置当前工作目录os。操作方法如下:
os
os.chdir(path)
import os # Specify the path you want to set as the current working directory new_directory = '/path/to/your/directory' # Change the current working directory os.chdir(new_directory) # Verify the change current_directory = os.getcwd() print(f"The current working directory is now: {current_directory}")
FileNotFoundError
os.chdir('..')