os.chdir(path) Change the current working directory to path.
Availability: Unix, Windows
EDIT
This will change the current working dir, you can get the current working by:
os.getcwd()
If you want to save it and restore it later, if you need to do some work in the original working dir.
EDIT 2
In any case you should probably move to subprocess (doc) as suggested here. If you use subprocess's Popen you have the choice of providing cwd parameter to specify the working directory for the subprocess: read this.
If cwd is not None, the child’s current directory will be changed to
cwd before it is executed. Note that this directory is not considered
when searching the executable, so you can’t specify the program’s path
relative to cwd.
Here, I made a little function to change the path you're working on :
import os
def make_path(r_path):
ack = 1
try:
root = os.path.dirname(__file__)
rel_path = os.path.join("..", r_path)
abs_path = os.path.join(root, rel_path)
os.chdir(abs_path)
ack = 0
except Exception as details:
print('problem to get to the path '+r_path+' (0001) : ' + str(details))
return ack
So here, r_path is the relative path you want to go to. I added ".." to the path.join() methode so, if you're in a folder and want to exit it before searching for your path, it does it automatically. So, if your relative directory looks like this :