Skip to content

Commit 0f1013a

Browse files
committed
DirFS: Handle paths with no leading / (#1638)
1 parent 262f664 commit 0f1013a

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

fsspec/implementations/dirfs.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,12 @@ def _relpath(self, path):
6666
return path
6767
if path == self.path:
6868
return ""
69+
# S3FileSystem returns paths that do not start with a '/', so we
70+
# need to remove the leading '/' from self.path if there is one there
71+
# but not on the incoming path
6972
prefix = self.path + self.fs.sep
73+
if not path.startswith(self.fs.sep) and self.path.startswith(self.fs.sep):
74+
prefix = prefix[1:]
7075
assert path.startswith(prefix)
7176
return path[len(prefix) :]
7277
return [self._relpath(_path) for _path in path]

fsspec/implementations/tests/test_dirfs.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,18 @@ def test_path(fs, root, rel, full):
9090
assert dirfs._relpath(full) == rel
9191

9292

93+
@pytest.mark.parametrize(
94+
"root, rel, full",
95+
[
96+
("/root", "foo", "root/foo"),
97+
("/root", "foo", "/root/foo"),
98+
],
99+
)
100+
def test_path_no_leading_slash(fs, root, rel, full):
101+
dirfs = DirFileSystem(root, fs)
102+
assert dirfs._relpath(full) == rel
103+
104+
93105
def test_sep(mocker, dirfs):
94106
sep = mocker.Mock()
95107
dirfs.fs.sep = sep

0 commit comments

Comments
 (0)