Traceback (most recent call last):os.DirEntry('snap')
Does anyone have an idea for why this limitation and how to go around it.
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:
Traceback (most recent call last):os.DirEntry('snap')
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,
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:
Traceback (most recent call last):os.DirEntry('snap')
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?
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.
On 23 Dec 2023, at 09:48, Antoon Pardon via Python-list <[email protected]> wrote:
Because I have functions with DirEntry parameters.
On 23 Dec 2023, at 09:48, Antoon Pardon via Python-list
<[email protected]> 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.
| Sysop: | Keyop |
|---|---|
| Location: | Huddersfield, West Yorkshire, UK |
| Users: | 715 |
| Nodes: | 16 (2 / 14) |
| Uptime: | 06:51:56 |
| Calls: | 12,100 |
| Calls today: | 8 |
| Files: | 15,003 |
| Messages: | 6,517,926 |
| Posted today: | 1 |