[Python] How to create and write to a textiowrapper and readlines

Discussão em 'Python' iniciado por Stack, Março 16, 2025.

  1. Stack

    Stack Membro Participativo

    So I am trying to create a text io wrapper that I can then use readlines() from for a unittest. Here is my attempt, but when I run it the readlines() returns nothing:

    output = io.BytesIO()
    wrapper = io.TextIOWrapper(
    output,
    encoding='cp1252',
    line_buffering=True,
    )

    wrapper.write('Text1')
    wrapper.write('Text2')
    wrapper.write('Text3')
    wrapper.write('Text4')

    for line in wrapper.readlines():
    print(line)


    What do I need to change to get this output:

    Text1
    Text2
    Text3
    Text4

    Continue reading...

Compartilhe esta Página