Skip to content

Commit 56054c0

Browse files
Improve Performance on LocalFileSystem.ls() if detail=False (#1789)
Co-authored-by: Martin Durant <[email protected]>
1 parent 6cade57 commit 56054c0

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-7
lines changed

fsspec/implementations/local.py

+8-7
Original file line numberDiff line numberDiff line change
@@ -57,20 +57,21 @@ def rmdir(self, path):
5757

5858
def ls(self, path, detail=False, **kwargs):
5959
path = self._strip_protocol(path)
60-
info = self.info(path)
61-
if info["type"] == "directory":
60+
path_info = self.info(path)
61+
infos = []
62+
if path_info["type"] == "directory":
6263
with os.scandir(path) as it:
63-
infos = []
6464
for f in it:
6565
try:
66-
infos.append(self.info(f))
66+
# Only get the info if requested since it is a bit expensive (the stat call inside)
67+
# The strip_protocol is also used in info() and calls make_path_posix to always return posix paths
68+
info = self.info(f) if detail else self._strip_protocol(f.path)
69+
infos.append(info)
6770
except FileNotFoundError:
6871
pass
6972
else:
70-
infos = [info]
73+
infos = [path_info] if detail else [path_info["name"]]
7174

72-
if not detail:
73-
return [i["name"] for i in infos]
7475
return infos
7576

7677
def info(self, path, **kwargs):

fsspec/implementations/tests/test_local.py

+15
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,21 @@ def test_ls_on_file(tmpdir):
402402
assert fs.exists(resource)
403403
assert fs.ls(tmpdir) == fs.ls(resource)
404404
assert fs.ls(resource, detail=True)[0] == fs.info(resource)
405+
assert fs.ls(resource, detail=False)[0] == resource
406+
407+
408+
def test_ls_on_files(tmpdir):
409+
tmpdir = make_path_posix(str(tmpdir))
410+
fs = LocalFileSystem()
411+
resources = [f"{tmpdir}/{file}" for file in ["file_1.json", "file_2.json"]]
412+
for r in resources:
413+
fs.touch(r)
414+
415+
actual_files = fs.ls(tmpdir, detail=True)
416+
assert {f["name"] for f in actual_files} == set(resources)
417+
418+
actual_files = fs.ls(tmpdir, detail=False)
419+
assert set(actual_files) == set(resources)
405420

406421

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

0 commit comments

Comments
 (0)