Accept pathlib objects in addition to strings #146

Closed
opened 2022-07-02 15:14:16 +00:00 by Ghost · 1 comment

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.
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 2022-07-02 19:20:58 +00:00
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
No description provided.