Saturday, March 31, 2007

Python note: os.path.join unexpected result for paths beginning with "/" or "\"

Need to pay attention when joining paths beginning with "/" (or "\" for windows).

os.path.join("c:\abc", "def", "ghi") -> no problem, c:\abc\def\ghi

os.path.join("c:\abc", "\def") -> outputs "\def".

1 comment:

mr700 said...

It is expected - the second part is not relative path (starts with \), and you can not write

os.path.join("c:\abc", "\def")

it must be

os.path.join("c:\\abc", "\\def")

escaping, remember :-)