Get and Set Working Directory in Python

we will learn how to set working directory in python. We will also learn to get the current working directory in Python. Let’s discuss how to get and set working directory in python with an example for each.

Get Current Working directory in python:

import os
os.getcwd()

The above code gets the current working directory so the output will be

D:\Public\R SAS PGSQL\Python\Python Tutorial\

 

Set working directory in python:

import os
os.chdir("D:\\Public\\Python\\")

or

import os
os.chdir("D:/Public/Python/")

So the working directory will be set to the above mentioned path.

 

Get and Set Working Directory in Python - image previous-small-11-1 on http://www.datasciencemadesimple.com                                                                                                                Get and Set Working Directory in Python next