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?
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. Thanks – Niklaus