Reputation: 5039
I want to retrieve the list of files in a directory. What would be the fastest way to do so
using subprocess.Popen
or using os.listdir
. The directory contain 10000 of files. and this has to be done recursively to retrieve the list from the directory and its sub directories. I know we can use os.walk
to retrieve the contents of directories but os.walk
just not work for what I am suppose to do.
Thanks
Upvotes: 0
Views: 870
Reputation: 27734
os.listdir
is very likely to be compiled c that calls the same base libc system methods that ls
does.
In contrast, subprocess.Popen
forks a whole new process which is an expensive system operation and requires new file handles to deal with tty operations.
Upvotes: 3