[Python] Why does os.fdopen() ignore "mode" argument?

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

  1. Stack

    Stack Membro Participativo

    This piece of code when run on Python 2.7.16 and 3.8.3 produces different results:

    import tempfile
    import os

    fd, lockfile = tempfile.mkstemp()
    flags = os.O_RDWR | os.O_CREAT
    mode = 'w+b'

    fd = os.open(lockfile, flags)
    fileobj = os.fdopen(fd, mode)

    print(fileobj.mode)

    os.remove(lockfile)


    In 2.7 it prints w+b as expected but in 3.8 it prints rb+. Why wouldn't it be respecting the mode parameter in this way?

    I've tried manually creating a file to discount tempfile differences but still get the same result.

    I can't see anything obvious in the docs:


    Running on MacOS 10.14.6

    Continue reading...

Compartilhe esta Página