将 BytesIO 与 markdown 一起使用而不是在 Python 中打开/读取有什么好处吗?

Is there any advantage to using BytesIO with markdown instead of open / read in Python?

考虑以下代码:

from markdown import markdown

f = open('myfile.md', 'r')
html_text = markdown(f.read())
f.close()

使用 io.BytesIO 和 markdownFromFile 有速度优势还是劣势?还是洗头?

from markdown import markdownFromFile
from io import BytesIO

s = BytesIO()
markdownFromFile(input='myfile.md', output=s)
html_text = s.getvalue()
s.close()

提前感谢您提供任何信息。

如果你自己对它进行基准测试是最好的,但从它的外观来看我没有看到使用 BytesIO 有任何优势。不是读取文件并将其直接解析为字符串,而是首先读取文件并将其处理为 BytesIO 对象,然后使用 BytesIO.getvalue 获取所需的字符串。

前者也更容易阅读。可以通过以下方式变得更简单:

with open('myfile.md', 'r') as f:
    html_text = markdown(f.read())