def write(fobj, content, convert=True):
"""
Utility function used to write content to a file. Allow compatibility
between Python versions.
"""
# This function automatically converts strings to bytes
# if running under Python 3. Otherwise we cannot write
# to a file.
# First detect whether fobj requires binary stream
if hasattr(fobj, 'mode'):
# A file-like object
binary = 'b' in fobj.mode
else:
# A subclass of io.BufferedIOBase?
binary = isinstance(fobj, io.BufferedIOBase)
# If we are running under Python 3 and binary is required
if sys.version_info[:2] >= (3, 0) and convert and binary: # pragma: no cover
content = bytes(content, 'utf-8')
fobj.write(content)
baker.py 文件源码
python
阅读 77
收藏 0
点赞 0
评论 0
评论列表
文章目录