python - Pastebin pbwrap 在 python 3.9.5 上有 AttributeError: __enter__ with io.open()
问题描述
来自 pypi 的 pastebin 包 pbwrap 以前对我来说总是正常工作,但它不适用于 python 3.9.5。它无法读取输入文件并给出以下错误。
from pbwrap import Pastebin # https://pypi.org/project/pbwrap/
api_dev_key = r'your key'
username = r'your login'
password = r'your password'
pb = Pastebin(api_dev_key)
user_id = pb.authenticate(username, password)
### Pasting text to pastebin works as expected.
paste_url = pb.create_paste('this is a test', api_paste_private=1, api_paste_name='test')
print(paste_url)
### Make a simple text file to upload to pastebin
with open('test.txt', 'w') as f:
f.write('This is line #1\n')
f.write('This is line #2\n')
### When uploading a file to pastebin, an I/O error is returned from io.open() called from pbwrap
paste_url = pb.create_paste_from_file('test.txt', api_paste_private=1, api_paste_name='test')
print(paste_url)
### pbwrap.py", line 180, in create_paste_from_file
### with io.open(
### AttributeError: __enter__
解决方案
这原来是 2020 年模块升级为使用 utf-8 时引入的错误。我已经分叉了 pbwrap 源代码并提交了一个修复程序。需要将读取的文件移出 with io.open 的上下文。
推荐阅读
- python-3.x - “.DS”在python中的含义
- mysql - SQL中递归cte方法的控制流程是什么?
- ocr - “识别文本”选项在 Visual Recognition 节点中不可见
- django-rest-framework - 如何形成 JSON 对象,以便它可以被嵌套模型序列化器反序列化
- java - 如何在Android应用程序中更改按钮点击布局?
- python - 我无法使用 selenium 或 BeautifulSoup 或两者结合用于动态网站返回评论文本
- google-apps-script - 使用 Sheets.Spreadsheets.BatchUpdate 的 JSON 有效负载无效
- python - 是否可以创建 PyQt Web 应用程序?
- jupyter-notebook - 我在安装时打开的 Jupyter 笔记本中未显示 nbextensions 中的目录 2 (toc2) 中的一些设置更改
- bash - 重命名批处理文件,并在下划线前添加前导零