Search code examples
pythonbufferedreaderpytumblr

Pytumblr - TypeError: expected str, bytes or os.PathLike object, not BufferedReader


This error happens and I do not know why, it links back to the posting function. I originally thought it was because I used the wrong variable for the access to my Tumblr blog, but the same error happens as I put my blog URL.

import pytumblr
import os

# Authenticate via Tumblr API
client = pytumblr.TumblrRestClient(
    "consumer-key",
    'consumer-secret',
    'oauth-key',
    'oauth-secret',
)

# Set the directory containing the photos to upload
photo_directory = 'dir-to-photos'

# Iterate through the files in the directory
for filename in os.listdir(photo_directory):
    # Upload the photo to Tumblr
    photo = open(photo_directory +"/"+ filename, 'rb')
    response = client.create_photo('phantom-cass.tumblr.com', data=photo, tags=['photo dump'])
    photo.close()

When inputting the correct variables in the correct spot, it gives me this exception:

File "dir\to\program", line 20, in <module>
    response = client.create_photo('blog-name.tumblr.com', data=photo, tags=['photo dump'])
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "dir\to\pytumblr's\helpers.py", line 47, in add_dot_tumblr
    return fn(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^
  File "dir\to\pytumblr's\__init__.py", line 308, in create_photo
    return self._send_post(blogname, kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "dir\to\pytumblr's\__init__.py", line 549, in _send_post
    return self.send_api_request("post", url, params, valid_options)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "dir\to\pytumblr's\__init__.py", line 573, in send_api_request
    files = {'data': open(params['data'], 'rb')}
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: expected str, bytes or os.PathLike object, not BufferedReader

Like mentionned above, upon troubleshooting, I realised the blog name wasn't the error. I've also looked to similar questions on Stack, but it was of no help for me, besides, their error ended with "io.BUFFEREDREADER", not "BufferedReader". They could be the same, but for agrument's sake, I will assume not.


Solution

  • So, I see why someone downvoted it. The answer is simple, don't open the file, just give the directory to create_photo()