NOTE: Having found out about the very similarly named pathlibfs which offers much of the same functionality but for a different backend, I might scrap this.
NOTE 2: Also note that there is work underway to define an ABC for Python's own pathlib, which I'm sure will, if merged, immediately cause packages with brand new implementations of that interface for various filesystems to crop up, reducing the usefulness of PyFilesystem.
pathlib.Path
interface
for PyFilesystem.
See also: PyFilesystem/pyfilesystem2#238
PyFilesystem provides a unified interface for dealing with various kinds of (generalized) "filesystems", e.g. the regular local filesystem, a virtual in-memory filesystem, filesystems on remote machines, ...
Meanwhile, a lot of libraries already use pathlib.Path
for dealing with
operations on the local filesystem.
Now if only there was a wrapper around PyFilesystem paths exposing a
pathlib.Path
interface, all of these libraries would automatically gain
support for the various filesystems supported by PyFilesystem, so long as they
consequently deal with paths only via pathlib.Path
methods (and don't
convert the path to a string before opening, etc.).
That's what this project aims to implement.
Currently, you can only install it from git. I'll have to rename it if I want to release it on PyPI (see "Related projects") below.
To start with, the API will be as close as possible to pathlib.Path
's, the
only exception being the constructor, which reads:
FsPath(fs, *path_segments)
fs
: PyFilesystem FS object*path_segments
: same meaning as inpathlib.Path
Note that path_segments
must be relative, as paths in PyFilesystem are
always relative to the FS object.
Additional properties and methods implemented on FsPath
are:
relative_fs_path
- the path relative to its FS object (i.e. the one specified by*path_segments
in the constructor)as_pathlib_path()
- returns the path'spathlib.Path
equivalent if possible (i.e. only works for OSFS and analogous FS types)
Eventually, there will also be extensions beyond the pathlib.Path
interface
for "missing" (in my opinion, anyway) functionality such as copying.
Whether these will be added in the form of subclass methods etc. is still TBD.
Work-in-progress & not suitable for production use yet.
I'm just going through pathlib.Path
's methods and re-implementing them in
the wrapper one by one (cf. list below).
If you need any of the ones that aren't implemented yet, feel free to open a ticket and I'll prioritize those.
- Done
as_posix
: doneas_uri
: donecwd
: doneexists
: doneexpanduser
: doneglob
: done, but yields only files (PyFilesystem/pyfilesystem2#389)group
: donehome
: doneis_block_device
: doneis_char_device
: doneis_dir
: doneis_fifo
: doneis_file
: doneis_socket
: doneis_symlink
: doneiterdir
: donejoinpath
: donelstat
: donemkdir
: donematch
: done (implicitly; only performed on the FS-relative path)name
: done (implicitly)open
: doneowner
: doneparent
: doneparents
: doneparts
: done (implicitly)read_bytes
: done (implicitly)read_text
: done (implicitly)relative_to
: done (with caveats)rename
: done (with caveats)rmdir
: donestat
: donestem
: done (implicitly)suffix
: done (implicitly)suffixes
: done (implicitly)touch
: doneunlink
: donewith_name
: donewith_suffix
: donewrite_bytes
: done (implicitly)write_text
: done (implicitly)
- To do
is_mount
: ?is_reserved
: ?replace
: ?rglob
: ?samefile
: ?
- To do, not clear what should happen
anchor
: ? (same issue asdrive
)absolute
: ?chmod
: blocked by unavailability in PyFilesystem itself...drive
: ?is_absolute
: ?lchmod
: blocked by unavailability in PyFilesystem itself...root
: ?resolve
: ?symlink_to
: blocked by unavailability in PyFilesystem itself...
- Done
__new__
: done__eq__
: done__repr__
: done__str__
: done (extension: can be forbidden)__truediv__
: done
- No point doing
__class__
: Python internal__delattr__
: Python internal__dir__
: Python internal__doc__
: Python internal__getattribute__
: Python internal__init__
: already covered by__new__
__init_subclass__
: Python internal__module__
: Python internal__setattr__
: Python internal__subclasshook__
: Python internal__slots__
: Python internal__sizeof__
: Python internal
- To do
__bytes__
: ?__enter__
: ?__exit__
: ?__format__
: ?__fspath__
: ?__ge__
: ?__gt__
: ?__hash__
: ?__le__
: ?__lt__
: ?__ne__
: ?__reduce__
: ?__reduce_ex__
: ?
- To do, not clear what should happen
__rtruediv__
: probably doesn't make sense?
I don't think I should implement any of those... but for completeness's sake:
_accessor
: ?_cached_cparts
: ?_closed
: ?_cparts
: ?_drv
: ?_format_parsed_parts
: ?_from_parsed_parts
: ?_from_parts
: ?_hash
: ?_init
: ?_make_child
: ?_make_child_relpath
: ?_opener
: ?_parse_args
: ?_parts
: ?_pparts
: ?_raise_closed
: ?_raw_open
: ?_root
: ?_str
: ?
- pathlibfs: the exact same idea as this, except instead of PyFilesystem FSs, it uses fsspec filesystems as its backends. The name is so similar that I'll have to rename this if I ever want to release it to PyPI (as it won't allow two very similarly named projects to coexist). See here for a comparison between fsspec and PyFilesystem.
- plumbum: has
LocalPath
andRemotePath
classes, which are almost (but not quite yet) compatible withpathlib.Path
. If you just need a local/remote abstraction, these can be used for much the same purposes as this project here. - fspatch: from what I understand, the aim of this is to be able to patch Python standard library modules to use PyFilesystem internally. If this is ever finished, it would probably make this wrapper pointless (?)