[issue28530] Howto detect if an object is of type os.DirEntry

Eryk Sun report at bugs.python.org
Mon Oct 31 19:53:31 EDT 2016


Eryk Sun added the comment:

To clarify, DirEntry is only exposed in the posix/nt and os modules starting in 3.6. To get a reference to it in 3.5 you have to fall back on something like the following:

    import os

    try:
        from os import DirEntry
    except ImportError:
        import tempfile
        with tempfile.NamedTemporaryFile() as ftemp:
            scan = os.scandir(os.path.dirname(ftemp.name))
            DirEntry = type(next(scan))
        del scan, ftemp, tempfile

In 3.5 os.scandir does not support the with statement or raise a resource warning. That behavior was added in 3.6, for which the workaround shouldn't be required.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue28530>
_______________________________________


More information about the Python-bugs-list mailing list