reading directory entries one by one
Michael Hudson
mwh at python.net
Wed May 22 06:21:47 EDT 2002
"Michael P. Soulier" <msoulier at nortelnetworks.com_.nospam> writes:
> Greetings.
>
> I'm aware that one can iterate over a directory's contents via the
> listdir() function in the os module.
>
> for file in os.listdir(dir):
>
> However, this does read in the entire directory into memory. As a C coder,
> I can opendir and readdir and iterate the directory file by file, in case
> sucking the entire thing into memory would be too resource intensive on large
> directories.
How big are your directories? I'd be surprised to find this was ever
an issue, but...
> Is there such an option for Python?
Don't think so. Wouldn't be that hard, though you'd have to write
some C. Hmm, might be a cool application of iterators.
Hmm, thinking aloud, would it be possible/nice to be able to do:
for entry in dir("/"):
if entry.isdir():
print "skipping", entry.basename
elif entry.islink():
print entry.readlink()
else:
print entry.basename, len(entry.open('r').read())
? Like I said, hmm.
Cheers,
M.
--
... Windows proponents tell you that it will solve things that
your Unix system people keep telling you are hard. The Unix
people are right: they are hard, and Windows does not solve
them, ... -- Tim Bradshaw, comp.lang.lisp
More information about the Python-list
mailing list