Skip to content

Is there a way to check whether two paths (or filesystems) are referencing the same file? #515

@tfeldmann

Description

@tfeldmann

I'd like to know if there is a way to check whether two pathes reference the same file.
I found that not even the filesystems can be compared:

>>> import fs
>>> fs1 = fs.open_fs("~/Desktop")
>>> fs2 = fs.open_fs("~/Desktop")
>>> fs1 == fs2
False

At the moment I'm resorting to this, which is not nice and complete at all.

from typing import NamedTuple

class Resource(NamedTuple):
    fs: FS
    path: str

def is_same(a: Resource, b: Resource):
    from fs.osfs import OSFS
    from fs.zipfs import ZipFS
    from fs.tarfs import TarFS

    if isinstance(a.fs, OSFS) and isinstance(b.fs, OSFS):
        return a.fs.getospath(a.path) == b.fs.getospath(b.path)
    elif isinstance(a.fs, ZipFS) and isinstance(b.fs, ZipFS):
        return a.fs._file == b.fs._file and a.path == b.path
    elif isinstance(a.fs, TarFS) and isinstance(b.fs, TarFS):
        return a.fs._file == b.fs._file and a.path == b.path
    return False

Is there a nicer way to check this?
Would you be interested in a PR that fixes the __eq__ method for some filesystems?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions