Platform-independent file paths?

Hairo picture Hairo · May 17, 2011 · Viewed 38.9k times · Source

How can I use a file inside my app folder in Python? Platform independent of course... something similar to this:

#!/bin/sh
mypath=${0%/*}
LIBDIR=$mypath/modules

Answer

Blender picture Blender · May 17, 2011

You can use os.path and its functions, which take care of OS-specific paths:

>>> import os
>>> os.path.join('app', 'subdir', 'dir', 'filename.foo')
'app/subdir/dir/filename.foo'

On Windows, it should print out with backslashes.