If you want to be super safe, you should use repeatedly os.path.split, or test for both os.sep and os.altsep as separator.
__file__ will always use os.sep, but when you get a path from other code or by direct input from a user, chances are that os.altsep ('/' on Windows) is used.
Example on Windows:
>>> path = os.path.join('c:\\a', 'c/d')
>>> print(path)
'c:\\a\\c/d'
>>> pathsep = re.compile(r'\\|/')
>>> pathsep.split(path)
['c:', 'a', 'b', 'c']
Alternatively:
def safe_split(path):
res = []
while True:
path, tail = os.path.split(path)
if len(tail) == 0:
break
res.append(tail)
res.append(path) # do not forget the initial segment
res.reverse()
return res
and
>>> safe_split('c:\\a\\c/d')
['c:\\', 'a', 'c', 'd']