Can Python3's pathlib be used portably between Linux and Windows systems?
Asked Answered
N

2

15

I'm writing some software in Python3 intended to take a backup of a directory structure on a Windows client, and send it to a Linux server.

The problem I'm having is how to deal with Windows and Linux file paths. I need the Windows client to create an object representing the relative path to the source file, send that relative path to the server, so the server knows which subdirectory to write the file to in the destination folder, and then send the actual data.

Sending the actual data is not a problem, but how do I send a Windows relative path to a Linux system? I tried doing it as strings, using os.path, but that quickly became a mess. I'm looking at using pathlib.

If I can create a path object of some sort, I can use pickle to serialize it, and send it to the server. What object would I use from pathlib to represent the path though?

Path() seems to create an instance of the class that works for the current filesystem (PosixPath or WindowsPath), which aren't portable. If I make a WindowsPath object on the Windows client, Linux won't be able to deserialize it, as you can't even instantiate a WindowsPath object on a Linux system.

It looks like I could use a PureWindowsPath object, and send that to Linux, but how do I convert the PureWindowsPath object (that represents a relative path) to either a PosixPath, or at least a PurePosixPath?

Is that possible? Or am I thinking about it all wrong?

Niklaus answered 1/1, 2019 at 18:3 Comment(0)
P
16

pathlib is an OO library, not a communication protocol. Just send strings.

Windows can handle paths with forward slashes just fine, and both pathlib and os.path can normalise such paths for you. You probably want to normalise paths in your protocol to use POSIX path separators (forward slashes).

You can convert relative Windows paths to POSIX paths using pathlib.PurePosixPath(), for example:

>>> from pathlib import Path, PurePosixPath
>>> Path('relative\path\on\windows')
WindowsPath('relative\path\on\windows')
>>> PurePosixPath(_)
PurePosixPath('relative/path/on/windows')
>>> str(_)
'relative/path/on/windows'

If your server receives a relative paths using Windows conventions, use PureWindowsPath() to convert it to a pathlib object, then pass that to Path() to convert:

>>> from pathlib import Path, PureWindowsPath
>>> received = 'relative\path\on\windows'
>>> PureWindowsPath(received)
PureWindowsPath('relative/path/on/windows')
>>> Path(_)
PosixPath('relative/path/on/windows')

If you keep paths as strings, then you can use str.replace(os.altsep, os.sep) to achieve the same on the Windows side before sending:

>>> import os
>>> p = 'relative\path\on\windows'
>>> p.replace(os.altsep, os.sep)
'relative/path/on/windows'

and you can always just use relative_path.replace('\\', os.sep) to force the issue.

Pigeonhole answered 1/1, 2019 at 18:15 Comment(3)
relative_path.replace('\\', '/') is what I was using in the first place, but my code is more advanced than the simple example above, doing a differential backup, sending file paths back and forth between Windows and Linux, and it became a mess. I see what you mean about sending strings, and converting them when you receive them. I'm going to give it a try. ThanksNiklaus
Note that "replace" is a function of the string object. The pathlib.Path.replace function will unconditionally move the object to the supplied argument.Laudianism
@Hydrox24: that's why the sentence used the phrase to do the same with strings; I edited that line to make this clearer.Pigeonhole
A
1

.as_posix() is way forward.

from pathlib import Path

p = Path('./my_dir/my_file.txt')
print(p)
# my_dir\my_file.txt
print(p.as_posix())
# my_dir/my_file.txt

Arenaceous answered 12/8 at 12:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.