[Tutor] Viewing Directories

David Porter jcm@bigskytel.com
Thu, 14 Dec 2000 20:58:02 -0700


* Timothy M. Brauch <tbrauch@mindless.com>:

> Is there anyway to view the contents of a folder in Windows 95/98 using
> Python?

import os
d = os.listdir('c:/dirname')
print d

> Ideally I would actually like to read the names of files in a folder and
> write the names to a file.

import os, string
d = os.listdir('c:/dirname')
f = open('c:/myfile.txt', 'w')
f.write(string.join(d, '\n')

Note that if you use backslashes (\) in paths, you need to escape them with
another backslash, e.g., c:\\dirname .  


David