Skip to content

Commit

Permalink
Improve Performance on LocalFileSystem.ls() if detail=False (#1789)
Browse files Browse the repository at this point in the history
Co-authored-by: Martin Durant <martin.durant@alumni.utoronto.ca>
  • Loading branch information
FrankEssenberger and martindurant authored Feb 25, 2025
1 parent 6cade57 commit 56054c0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
15 changes: 8 additions & 7 deletions fsspec/implementations/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,20 +57,21 @@ def rmdir(self, path):

def ls(self, path, detail=False, **kwargs):
path = self._strip_protocol(path)
info = self.info(path)
if info["type"] == "directory":
path_info = self.info(path)
infos = []
if path_info["type"] == "directory":
with os.scandir(path) as it:
infos = []
for f in it:
try:
infos.append(self.info(f))
# Only get the info if requested since it is a bit expensive (the stat call inside)
# The strip_protocol is also used in info() and calls make_path_posix to always return posix paths
info = self.info(f) if detail else self._strip_protocol(f.path)
infos.append(info)
except FileNotFoundError:
pass
else:
infos = [info]
infos = [path_info] if detail else [path_info["name"]]

if not detail:
return [i["name"] for i in infos]
return infos

def info(self, path, **kwargs):
Expand Down
15 changes: 15 additions & 0 deletions fsspec/implementations/tests/test_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,21 @@ def test_ls_on_file(tmpdir):
assert fs.exists(resource)
assert fs.ls(tmpdir) == fs.ls(resource)
assert fs.ls(resource, detail=True)[0] == fs.info(resource)
assert fs.ls(resource, detail=False)[0] == resource


def test_ls_on_files(tmpdir):
tmpdir = make_path_posix(str(tmpdir))
fs = LocalFileSystem()
resources = [f"{tmpdir}/{file}" for file in ["file_1.json", "file_2.json"]]
for r in resources:
fs.touch(r)

actual_files = fs.ls(tmpdir, detail=True)
assert {f["name"] for f in actual_files} == set(resources)

actual_files = fs.ls(tmpdir, detail=False)
assert set(actual_files) == set(resources)


@pytest.mark.parametrize("file_protocol", ["", "file://"])
Expand Down

0 comments on commit 56054c0

Please sign in to comment.