There has been multiple discussions about the issue when dealing with trailing slashes in pathlib.Path
, on Unix systems in particular such as https://bugs.python.org/issue21039 and https://bugs.python.org/issue39140.
Given the pathlib.Path
constructed from a string, I wonder what would be the best way to make sure a trailing slash is preserved in the Path
object the same way the os
module does it?
>>> os.path.dirname("/a/b/")
'/a/b'
>>> os.path.dirname("/a/b")
'/a'
os
module understands the difference between "/a/b/"
and "/a/b"
, but pathlib
doesn't:
>>> Path("/a/b/").parent
PosixPath('/a')
Is there any way to be able to differentiate between paths that are pointing to a file (without a trailing slash) and to a directory (that has a trailing slash)? Or I'd have to switch to using os
module in this particular case?
If it's not possible, what would be a reasonable workaround to take advantage of pathlib
and deal with the trailing slash issue?