Newbie Question: Getting a list of files

Steven D'Aprano steven at REMOVE.THIS.cybersource.com.au
Wed May 16 00:33:37 EDT 2007


On Tue, 15 May 2007 17:12:01 -0700, Brian wrote:

> How do I, in Python, obtain a recursive list of files in a specified
> directory, including the subdirectories, etc?  For example, in the old
> MS-DOS days, we could specify at the command prompt "DIR /S" and this
> would provide a listing of all files, including those in subdirectories,
> no matter how far down the branch they were.  How can this be done with
> Python?

In MS DOS days, you were dealing with a small number of files. Python has 
to deal with file systems that could contain billions of files. If each 
file name has an average of eight characters, your recursive list of 
files could require a gigabyte of memory just to hold the file names. (It 
might not, of course -- it depends on the file system in use.)

I suggest you have a look at the os module, in particular os.listdir and 
os.walk.


-- 
Steven.



More information about the Python-list mailing list