ISquared
ISquared

Reputation: 462

os.chdir() gives NoneType object

I am changing my working directory to make sure images are loaded from the right place.

import os 
cwd = os.getwd()    ##works fine 
newwd = os.chdir("C:\\Users\\Me\\Python\\Images")     ##gives NoneType object

I never had problems with that before. Now as opposed to saving the variable as a string (i.e. the path I specified), it saves it as a NoneType of size = 1, so it is essentially empty. I tried with \, //, /, \, single or double quotations. I copied even the line of code I used before, from a previous python file, and it still does the same.

Upvotes: 2

Views: 698

Answers (1)

CDJB
CDJB

Reputation: 14536

As described in the docs here, os.chdir(path) changes the current working directory to the path specified. It doesn't return anything, hence why you get newwd = None. You can just run

os.chdir("C:\\Users\\Me\\Python\\Images")

to change the working directory. Then os.getcwd() will return "C:\\Users\\Me\\Python\\Images".

Upvotes: 5

Related Questions