About os.path.join and the / or \


(Manuel) #1

If I use os.path.join function under win (linux not tested) to join,
for example:

“C/folder/folder1/” and “icons”

I’ve a correct result:

“C/folder/folder1/icons”

But if I use the same function to join
(without / at the end of folder1)

“C/folder/folder1” and “icons”

I’ve this

“C/folder/folder1\icons”

This run without problem under win, but I’ve fear
this can create a problem under linux.

What’s the correct sintax under linux and window?
Where I must use / or \ or // or \ ??

thx,

  Manuel

(eeshlo) #2

What I usually do is use something like:


filename = filename.replace('\\', '/')

That way it is all replaced with a single ‘/’. On the other hand, I think that the blenderman script (at least older versions) and yable too, do tend to use a mix of both without problems.
If you want to make sure it is correct for the platform it is running on, you can also use a special function for that:


filename = os.path.normpath(filename)

Something like ‘dir\dir/file’ will then change to ‘dir\dir\file’ on windows, and ‘dir/dir/file’ on linux.


(Schlops) #3

IIRC os.path.join recognizes the platform, you don’t have to deal with path-seperators.


(theeth) #4

os.sep is the official path separator on the OS your are using.

Martin