Skip to content

How to work with libraries that expect a str or PathLike object? #552

@cyrillkuettel

Description

@cyrillkuettel

Whenever I work with other libraries that operate on a file inside a filesystem, I always find myself calling path.getsyspath
and passing that into some function of that library.

For example:

from fs.subfs import SubFS

archive_dir: SubFS = self.app.filestorage.makedirs("/parent")
archive_dir.create("archive.zip")

sys_path = archive_dir.getsyspath('archive.zip')
with ZipFile(sys_path, "w", ZIP_DEFLATED) as zip_file:
    for match in archive_dir.glob('**/*.csv'):
        zip_file.write(archive_dir.getsyspath(match.path))

This runs fine. However, if I don't use the getsyspath,

archive = archive_dir.open('archive.zip')
with ZipFile(archive, "w", ZIP_DEFLATED) as zip_file:
    for match in archive_dir.glob('**/*.csv'):
        zip_file.write(archive_dir.getsyspath(match.path))

I get:

TypeError: write() argument must be str, not bytes

I'm very much depending on getsyspath to open files with other libraries. Is there a way to do this without getsyspath?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions