What are you doing to each file in the directory? I think there isn't really a choice about using os.listdir, but depending on what you are doing, you might be able to process files in parallel. For example, we could use the Pool from the multiprocessing library to spawn off more Python processes, then have each process iterate over a smaller subset of the files.
http://docs.python.org/library/multiprocessing.html
This is kind of rough, but I think it gets the point across...
import sys
import os
from processing import Pool
p = Pool(3)
def work(subsetOfFiles):
for file in subsetOfFiles:
with open(file, 'r') as f:
#read file, do work
return "data"
p.map(work, [[#subSetFiles1],[#subSetFiles2],[#subSetFiles3]])
The general idea is to get the list of files from os.listdir, but instead of going over 100,000 files one by one, we split 100,000 files into 20 lists of 5,000 files, and process 5,000 files in each process. One of the nice things about this approach is it will benefit from the current trend of multicore systems.