How to Know and Change the Working Directory in Python [Example of getcwd(), chdir()]

with No Comments

Set Working Directory in Python

If you do not have the working folder set as the working directory, you can not import the data into Python.

In Spyder, we can set the working directory by simply clicking the “RUN” button or pressing F5 on keyboard

set working directory in python

After “RUN” is clicked, you should be able to see the working directory displayed in iPython console

set working directory - console

Set Working Directory using getcwd() and chdir()

To Find or Set the working directory, we need to do the following in console

import os

Know your directory

os.getcwd()

Set your directory

os.chdir(“XXX”)

where xxx is your directory path.

set working directory using getcwd chdir

Other Sections on Data Handling in Python

1.) How to Import Libraries 

2.) How to Know and Change the Working Directory 

3.) How to Import CSV Data using Pandas

4.) How to Set Dependent Variables and Independent Variables using iloc

5.) How to Handle Missing data with Imputer

6.) How to Set Categorical Data (Dummy Variable) using LabelEncoder and OneHotEncoder

7.) How to Split Data into Training Set and Testing Set

8.) How to Apply Feature Scaling

Leave a Reply