python / python/cpython

os.scandir(): add option to yield the given path itself

Aperta
#99,454 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

stdlib type-feature
Lingua principale
Python
Stelle
77.2k
Fork
36k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

Feature or enhancement

I think it would be nice if os.scandir() had an option (that defaults to the current behaviour) which makes it also yield the root path itself as given by its path argument.

The benefit would be, that if one has code like e.g.:

def scandirtree(path):
    for p in os.scandir(path):
        if p.is_dir(follow_symlinks=False):
            yield p
            yield from scandirtree(p)
        else:
            yield p

one would also yield path and process that as well (in whatever one does with the generator).

It doesn't seem to be possible right now to write a warpper around scandir which would just yield path first, since one cannot create os.DirEntry objects (see #71983).

Pitch

The above in turn would be the familiar behaviour of many tools, e.g.

$ rm -rf path

also removes the path itself, not just its contents.

find path

also finds path itself.
Etc. pp.

So anyone who wants to implement a tool like these or similar behaviour, would no longer need to specially handle the path itself.

Previous discussion

https://discuss.python.org/t/os-scandir-should-have-an-option-to-also-yield-the-path-itself/18383

Thanks,
Chris.

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia dal punto di ingresso os.scandir() ed esamina la limitazione correlata discussa nell’issue #71983 e nella discussione collegata. Definisci un comportamento opt-in che mantenga il comportamento predefinito attuale restituendo al contempo il percorso fornito stesso come risultato compatibile, quindi aggiungi una copertura che mostri entrambi i comportamenti.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
operating-systems
Tipo di issue
Funzionalità
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
32/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.