Skip to content

Improve Performance on LocalFileSystem.ls() if detail=False #1789

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading