I think this answers the questions why Python uses a particular separator.
That said, you can use the Pathlib
module to construct your paths and specify whether you want a Posix or Windows path.
Example:
from pathlib import PurePosixPath, PureWindowsPath
print(PurePosixPath('some', 'silly', 'long', 'path'))
>> some/silly/long/path
print(PureWindowsPath('some', 'silly', 'long', 'path'))
>> some\silly\long\path
Make sure you use the pure
version of PosixPath
and WindowsPath
. If you're trying to use WindowsPath
on a Posix system, you'll get the following error:
NotImplementedError: cannot instantiate 'WindowsPath' on your system
This is also specified in the docs:
If you want to manipulate Windows paths on a Unix machine (or vice versa). You cannot instantiate a WindowsPath
when running on Unix, but you can instantiate PureWindowsPath
.