Files
Python_CookBook_repo/5.文件与IO/6.在字符串上执行IO操作.py
2025-09-10 16:12:45 +08:00

27 lines
647 B
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import io
if __name__ == '__main__':
# io库里的StringIO和BytesIO提供了两个模拟文件的方法本质应该是在内存里整了块缓冲区
# StringIO提供了对字符串的虚拟IOBytesIO则对应字节串
# 这两个东西的操作和文件操作别无二致,就是不需要打开
s = io.StringIO()
b = io.BytesIO()
s.write("Hello_String_IO\n")
b.write(b'Hello BytesIO')
print("This is POWER!!!!!", file=s, end='')
print(s.getvalue())
s.seek(0)
print(s.read(4))
s.seek(0)
print(s.read())
print(b.getvalue())
b.seek(0)
print(b.read(4))
print(b.read())