Pruning with os.scandir?
Peter Otten
__peter__ at web.de
Thu Aug 6 08:29:49 EDT 2015
Stephen Kennedy wrote:
> I just saw PEP 471 announced. Mostly it looks great! One thing I found
> puzzling though is the lack of control of iteration. With os.walk, one can
> modify the dirs list inplace to avoid recursing into subtrees (As
> mentioned somewhere, in theory one could even add to this list though that
> would be a strange case).
>
> I can't see how to do this with os.scandir. I hope I am missing something?
> Don't make me walk the entire contents of .git, tmp and build folders
> please.
I think you misunderstood. scandir() is the generator-producing equivalent
of listdir() which returns a list. Neither of them recurses into
subdirectories:
$ tree
.
└── [4.0K] top
├── [4.0K] one
├── [4.0K] three
│ ├── [4.0K] bar
│ └── [4.0K] foo
└── [4.0K] two
6 directories, 0 files
$ python3.5
Python 3.5.0b2+ (3.5:9aee273bf8b7+, Jun 25 2015, 09:25:29)
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.listdir()
['top']
>>> os.scandir()
<posix.ScandirIterator object at 0x7f3900de7330>
>>> list(_)
[<DirEntry 'top'>]
The /implementation/ of walk() was modified to use the more efficient
scandir() under the hood, the user interface does not change:
>>> for path, folders, files in os.walk("."):
... try: folders.remove("three")
... except: pass
... for name in folders + files:
... print(os.path.join(path, name))
...
./top
./top/two
./top/one
More information about the Python-list
mailing list