• making your own DirEntry.

    From Antoon Pardon@21:1/5 to All on Fri Dec 22 13:00:52 2023
    I am writing a program that goes through file hierarchies and I am mostly
    using scandir for that which produces DirEntry instances.

    At times it would be usefull if I could make my own DirEntry for a specific path, however when I try, I get the following diagnostic:

    os.DirEntry('snap')
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    TypeError: cannot create 'posix.DirEntry' instances


    Does anyone have an idea for why this limitation and how to go around it.

    At this moment I don't consider pathlib very usefull, it lacks the follow_symlinks parameter in the is_dir, is_file, ... methods.

    --
    Antoon Pardon.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Paul Rubin@21:1/5 to Antoon Pardon on Fri Dec 22 11:20:22 2023
    Antoon Pardon <antoon.pardon@vub.be> writes:
    Does anyone have an idea for why this limitation and how to go around it.

    Make a subclass?

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From DL Neil@21:1/5 to Antoon Pardon via Python-list on Sat Dec 23 09:39:35 2023
    Antoon,


    On 12/23/23 01:00, Antoon Pardon via Python-list wrote:
    I am writing a program that goes through file hierarchies and I am mostly using scandir for that which produces DirEntry instances.

    At times it would be usefull if I could make my own DirEntry for a specific path, however when I try, I get the following diagnostic:

    os.DirEntry('snap')
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: cannot create 'posix.DirEntry' instances


    Does anyone have an idea for why this limitation and how to go around it.

    At this moment I don't consider pathlib very usefull, it lacks the follow_symlinks parameter in the is_dir, is_file, ... methods.


    Can't recall ever trying this.


    The manual (https://docs.python.org/3/library/os.html#os.DirEntry)
    suggests that a DirEntry is one of those Python data-constructs which it creates, but we may only use: "cannot create".

    Secondly, that a DirEntry object consists of a lot more than the directory-name, eg its path.

    Thirdly, that os.scandir() deals (only) with concrete directories -
    unlike pathlib's ability to work with both the real thing and abstract files/dirs.


    Why create a DirEntry? Why not go directly to os.mkdir() or whatever?

    --
    Regards =dn

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Antoon Pardon@21:1/5 to All on Sat Dec 23 10:48:43 2023
    Op 22/12/2023 om 21:39 schreef DL Neil via Python-list:
    Antoon,


    On 12/23/23 01:00, Antoon Pardon via Python-list wrote:
    I am writing a program that goes through file hierarchies and I am
    mostly
    using scandir for that which produces DirEntry instances.

    At times it would be usefull if I could make my own DirEntry for a
    specific
    path, however when I try, I get the following diagnostic:

    os.DirEntry('snap')
    Traceback (most recent call last):
       File "<stdin>", line 1, in <module>
    TypeError: cannot create 'posix.DirEntry' instances


    Does anyone have an idea for why this limitation and how to go around
    it.

    At this moment I don't consider pathlib very usefull, it lacks the
    follow_symlinks parameter in the is_dir, is_file, ... methods.


    Can't recall ever trying this.


    The manual (https://docs.python.org/3/library/os.html#os.DirEntry)
    suggests that a DirEntry is one of those Python data-constructs which
    it creates, but we may only use: "cannot create".

    Secondly, that a DirEntry object consists of a lot more than the directory-name, eg its path.

    Thirdly, that os.scandir() deals (only) with concrete directories -
    unlike pathlib's ability to work with both the real thing and abstract files/dirs.


    Why create a DirEntry? Why not go directly to os.mkdir() or whatever?

    Because I have functions with DirEntry parameters.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From immibis@21:1/5 to Antoon Pardon on Sat Dec 23 11:30:24 2023
    On 12/23/23 10:48, Antoon Pardon wrote:
    Op 22/12/2023 om 21:39 schreef DL Neil via Python-list:
    Why create a DirEntry? Why not go directly to os.mkdir() or whatever?

    Because I have functions with DirEntry parameters.


    Python is duck-typed, so it's quite likely that if you pass something
    that *looks like* a DirEntry - has the same variables and methods - it
    will work. If it walks like a DirEntry and quacks like a DirEntry, it's
    a DirEntry.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Barry Scott@21:1/5 to All on Sat Dec 23 11:34:58 2023
    On 23 Dec 2023, at 09:48, Antoon Pardon via Python-list <python-list@python.org> wrote:

    Because I have functions with DirEntry parameters.

    I would duck-type a class I control to be my DirEnrry in this situation.
    Would also help you when debugging as you can tell injected DirEntry from "real" DirEntry.

    Barry

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Antoon Pardon@21:1/5 to All on Sat Dec 23 13:21:47 2023
    Op 23/12/2023 om 12:34 schreef Barry Scott:


    On 23 Dec 2023, at 09:48, Antoon Pardon via Python-list
    <python-list@python.org> wrote:

    Because I have functions with DirEntry parameters.

    I would duck-type a class I control to be my DirEnrry in this situation. Would also help you when debugging as you can tell injected DirEntry
    from "real" DirEntry.

    Yes that seems to be, the way to go.

    --
    Antoon Pardon

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)