14

I am using pathlib to manage my paths in my Python project using the Path class.

When I am using Linux, everything works fine. But on Windows, I have a little issue.

At some point in my code, I have to write a JavaScript file which lists the references to several other files. These paths have to be written in POSIX format. But when I do str(my_path_instance) on Windows, The path is written in Windows format.

Do you know a simple way to convert a WindowsPath to a PosixPath with pathlib?

sophros
  • 14,672
  • 11
  • 46
  • 75
graille
  • 1,131
  • 2
  • 14
  • 33

2 Answers2

27

pathlib has an as_posix method to convert from Windows to POSIX paths:

pathlib.path(r'foo\bar').as_posix()

Apart from this, you can generally construct system-specific paths by calling the appropriate constructor. The documentation states that

You cannot instantiate a WindowsPath when running on Unix, but you can instantiate PureWindowsPath. [or vice versa]

So use the Pure* class constructor:

str(pathlib.PurePosixPath(your_posix_path))

However, this won’t do what you want if your_posix_path contains backslashes, since \ (= Windows path separator) is just a regular character as far as POSIX is concerned. So a\b is valid POSIX filename, not a path denoting a file b inside a directory b, and PurePosixPath will preserve this interpretation:

>>> str(pathlib.PurePosixPath(r'a\b'))
'a\\b'

To convert Windows to POSIX paths, use the PureWindowsPath class and convert via as_posix:

>>> pathlib.PureWindowsPath(r'a\b').as_posix()
'a/b'
Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
  • Thanks for your reply, but it doesn't seem to work :( On my python console : >>> t = Path("foo") / "bar" >>> str(pathlib.PurePosixPath(t)) . Output : 'foo\\bar' – graille Feb 13 '19 at 14:52
  • @graille Odd, it works for me (the other way round; I’m on macOS), and it *should* work. That said, see update. There’s a more direct way. – Konrad Rudolph Feb 13 '19 at 16:39
  • `as_posix` is the correct method here. I can verify that `str(PurePosixPath("some\\path\\on\Windows"))` still uses backslashes, though. – Adam Smith Feb 13 '19 at 17:01
  • 1
    @AdamSmith That … must be a bug in pathlib. On macOS I get, as expected `str(PureWindowsPath(Path('foo/bar'))) == 'foo\\bar'`, and this is indeed the documented behaviour (although the documentation doesn’t explicitly mention the `__str__` dunder method, but the documentation comment for `Path.__str__` *does* specify this behaviour). – Konrad Rudolph Feb 13 '19 at 17:55
  • @KonradRudolph I'm inclined to agree! – Adam Smith Feb 13 '19 at 18:06
  • I’ve changed my mind re: my last comment. This is almost certainly *not* a bug. It works in the direction POSIX➝Windows because `/` is a valid path separator. But `\\` in POSIX is *not* a path separator; instead, it’s a regular path character, so *obviously* `PurePosixPath(r'a\b')` is going to return a (valid) POSIX path containing a *backslash*. – Konrad Rudolph Dec 22 '20 at 11:23
  • Typo: `pathlib.path` should be `pathlib.Path`. – BSalita Jul 10 '21 at 17:43
  • beware that as_posix() returns a string, not a PurePosixPath object – neves Oct 13 '21 at 23:47
  • Can we change from PosixPath to WindowsPath? Anyone? – curiouscheese Aug 05 '22 at 09:24
3

Python pathlib 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/PurePosixPath

.

enter image description here

Tamara Koliada
  • 1,200
  • 2
  • 14
  • 31
  • I don't understand, could you please elaborate? – Ayush Sep 03 '21 at 12:49
  • I was looking through the **tensorflow** repository source code and I found this statement `os.path.abspath(path).replace('\\', '/')` and this is the only statement, it is a single line function they are using to convert `posix` path to `windows` path and now I'm kind of confused that how come this statement returns a valid `windows` path. – Ayush Sep 03 '21 at 13:05
  • 1
    windows accepts "/" as valid paths in the NT apis, but not on the older Win32 ones, so it depends on how the application consuming those paths are made – Luiz Felipe Aug 01 '22 at 19:32