How to Set Working Directory in Python
To change the current working directory(CWD), os.chdir()
method in the OS module is used. This method changes the CWD to a specified path. It takes a single argument as a new directory path. os.getcwd()
is used to get the current working directory.
The current working directory is the folder in which the Python script is operating.
import os print("Current working directory - before") print(os.getcwd()) print() os.chdir('../') print("Current working directory - after") print(os.getcwd())
Output:
Current working directory - before C:\Users\user\Desktop Current working directory - after C:\Users\user
Subscribe
Login
Please login to comment
0 Discussion