Accept pathlib objects in addition to strings #146

Closed
opened 2 years ago by Ghost · 1 comments
Ghost commented 2 years ago

Since version 3.4 (2014), Python has shipped with pathlib which provides objects to make handling paths easy and consistent across platforms. Currently, if you pass a Path object to img2pdf, you receive this error:

TypeError: Neither implements read() nor is str or bytes

You have to loop through the Path objects and convert them with str() before passing them to img2pdf. It would be great if users could pass Path objects directly.

Since version 3.4 (2014), Python has shipped with [`pathlib`](https://docs.python.org/3/library/pathlib.html) which provides objects to make handling paths easy and consistent across platforms. Currently, if you pass a `Path` object to `img2pdf`, you receive this error: ``` TypeError: Neither implements read() nor is str or bytes ``` You have to loop through the `Path` objects and convert them with `str()` before passing them to `img2pdf`. It would be great if users could pass `Path` objects directly.
josch commented 2 years ago
Owner

You don't hve to convert them with str(). You can either:

  • call pathlib.Path.open() and pass the resulting object to img2pdf (but don't forget to close the resulting object)
  • call pathlib.Path.read_bytes() and pass the resulting byte string to img2pdf -- img2pdf will slurp the file contents into memory anyways

But I agree that it should be possible to just pass a pathlib.Path object without having to jump through any extra hoops.

In 272fe0433f I added that img2pdf will also accept objects that have a member function called read_bytes which covers pathlib.Path objects but also anything else implementing that function.

Thanks!

You don't hve to convert them with `str()`. You can either: - call `pathlib.Path.open()` and pass the resulting object to `img2pdf` (but don't forget to close the resulting object) - call `pathlib.Path.read_bytes()` and pass the resulting byte string to `img2pdf` -- img2pdf will slurp the file contents into memory anyways But I agree that it should be possible to just pass a `pathlib.Path` object without having to jump through any extra hoops. In 272fe0433fd3bbbb839153b8d1af957136aac0f9 I added that img2pdf will also accept objects that have a member function called `read_bytes` which covers `pathlib.Path` objects but also anything else implementing that function. Thanks!
josch closed this issue 2 years ago
Sign in to join this conversation.
No Milestone
No project
No Assignees
2 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: josch/img2pdf#146
Loading…
There is no content yet.