appending sys.path with a relative path

Asked by Lejma

hi guys, im working on something and i got stuck, for now i will simplify the problem to the point which i found most suitable for my needs but i cannot get over it...

so the short story is, i need to append sys.path by a relative path to the module i just called, i tried to append sys.path by various ways found over the internet, mostly stackoverflow, and usualy they advice of getting current path or working directory and using os.path.join() construct to join it with >> ".." << which should resemble one directory above, but it does not work.

what i end up with, when using os.path.join("C:\", "xy", mydir", ".."), is this: "C:\xy\mydir\.." so it just takes the two dots as a string and does not handle them in any special way... i work on windows and to explain further, i need to import a module from directory level or two above... i spent 6 hours on it, i got several workarounds but this would solve it in a much nicer way and im also confused about why it does not work if it did for a lot of people...

thank you for any hint!

Question information

Language:
English Edit question
Status:
Solved
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Solved by:
RaiMan
Solved:
Last query:
Last reply:
Revision history for this message
Best RaiMan (raimund-hocke) said :
#1

if it helps:
os.path.dirname(currentPath)

strips the last portion in the path, that is not terminated with a slash/backslash.

BTW: you can work totally with forward slashes instead of backslashes to avoid the various methods to tell Jython about backslashes.

so
print os.path.dirname("C:/xy/mydir")

should print
C:/xy

Revision history for this message
Lejma (dan-svoboda) said :
#2

Thanks RaiMan, that solved my question.

Revision history for this message
Lejma (dan-svoboda) said :
#3

thank you RaiMan, this helped a lot!

I had it a bit more complicated, i was calling a script from remote workstation via PSexec, hence currentPath was something crazy, so i combined getBundlePath() with os.path.dirname() to reach what i wanted, i needed one level above, and two levels above, hence for two levels above i used this:

os.path.dirname(os.path.dirname(getBundlePath()))

thank you again!