I ran into something interesting when bug testing some code, when i use os.chdir('/home') for example python will still show my cwd as whatever i started the interpreter from. Where it gets weird is the dir is actually being changed but the interpreter never shows this.
Is this some weird legacy of 2.7~ or is this something working as intended? I spent a while this morning trying to figure out why my directory was never changing inside the interpreter when it actually was.
Im using functions from import os, import sys and basic python commands.
Interpreter Setup: import os import sys
def findAHomeP(homeDir="randomDirName"):
cwd = os.cwd()
splitCwd = cwd.split('/')
try:
index = splitCwd.index(homeDir)
except NameError as e:
print "error stuff"
return cwd
newPath = '/'.join(splitCwd[0:index+1])+'/'
return newPath
This code roughly returns a home dir for the desired location. Basically i was writing a test case to test this and make sure the results returned correctly. When i was going into the interpreter to test things before going to the next step i found that when you use the os.chdir(path) command it doesn't actually show the dir change in the python interpreter, it will still show whatever directory you started the interpreter from. E.g if i start the interpreter in /home/user/dir1/dir2/d5 it will always display that directory inside the interpreter if you use cwd. Even if you have changed it with os.chdir(newPath) which does work but the interpreter doesn't update the cwd for some reason.