You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Pytumblr调用create_photo时报TypeError:预期路径对象而非BufferedReader

解决pytumblr create_photo接口的TypeError问题

问题描述

调用pytumblr的create_photo接口上传图片时触发TypeError,错误提示:

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

最初怀疑是博客访问变量错误,替换为博客URL后问题仍存在,参考同类问题未得到有效解决,且报错结尾为BufferedReader,与常见的io.BufferedReader略有差异。

原实现代码

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()

完整报错堆栈

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

问题分析

从报错堆栈的关键行files = {'data': open(params['data'], 'rb')}可以看出:pytumblr库的create_photo接口要求data参数传入文件路径字符串,而非已经打开的BufferedReader对象。你提前打开了文件并传递句柄,库内部还会尝试调用open()去处理这个句柄,因此触发类型错误。

解决方案

直接传递文件的完整路径字符串给data参数,无需提前打开文件。同时使用os.path.join()拼接路径,避免跨平台路径分隔符问题。

修正后的代码

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):
    # 用os.path.join拼接完整文件路径,适配不同操作系统
    photo_path = os.path.join(photo_directory, filename)
    # 直接传递文件路径给data参数
    response = client.create_photo('phantom-cass.tumblr.com', data=photo_path, tags=['photo dump'])

说明

  • 移除了手动打开文件的代码,让pytumblr库内部自行处理文件的打开与读取逻辑
  • os.path.join()会根据操作系统自动选择正确的路径分隔符(Windows用\,Linux/macOS用/),避免路径拼接错误

内容的提问来源于stack exchange,提问作者Cassidy Mettraux

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 10:49:54