coanor
coanor

Reputation: 3962

Python-2.x: list directory without os.listdir()

With os.listdir(some_dir), we can get all the files from some_dir, but sometimes, there would be 20M files(no sub-dirs) under some_dir, these would be a long time to return 20M strings from os.listdir().

(We don't think it's a wise option to put 20M files under a single directory, but it's really there and out of my control...)

Is it any other generator-like method to do the list operation like this: once find a file, yield it, we fetch it and then the next file.

I have tried os.walk(), it's really a generator-style tool, but it also call os.listdir() to do the list operation, and it can not handle unicode file names well (UTF-8 names along with GBK names).

Upvotes: 5

Views: 1583

Answers (1)

Saleem
Saleem

Reputation: 8988

If you have python 3.5+ you can use os.scandir() see documentation for scandir

Upvotes: 3

Related Questions