Can pyfilesystem combine schemas?

57 Views Asked by At

I would like to use pyfilesystem with a combination of schemas. For example, I want to open a tar file on an FTP server and I would do ftp+tar://user:password@host:port/path/file.tar.gz.

2

There are 2 best solutions below

0
On BEST ANSWER

Not as such, however the TarFS constructor accepts an open file. So something along these lines should work:

with open_fs("ftp://user:password@host:port/") as ftp_fs:
    with ftp_fs.open("path/file.tar.gz") as tar_file:
        my_tar = TarFS(tar_file)
        my_tar.tree()
0
On

I ended up with something like this:

@contextmanager
def open_url(url: str,
             mode: str = "r",
             create: bool = False,
             buffering: int = -1,
             encoding: Optional[str] = None,
             errors: Optional[str] = None,
             newline: str = "",
             **options: Any,
    ) -> typing.IO:
    writeable = True if "w" in mode else False
    dir_url, file_name = os.path.split(url)
    with open_fs(dir_url, writeable, create) as fs_:
        with fs_.open(file_name, mode, buffering, encoding, errors, newline,
                      **options) as file_:
            yield file_