Anurag Sharma
Anurag Sharma

Reputation: 5039

Which one is the faster way to get the list of directories subprocess.Popen or os.listdir

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

Answers (1)

Alastair McCormack
Alastair McCormack

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

Related Questions