os.listdir: which ones are directories?

Mark McEahern marklists at mceahern.com
Tue Jan 15 00:15:35 EST 2002


Short answer:  os.path.isdir() and os.path.isfile() are your friends.

// mark

$ python
Python 2.2 (#1, Dec 31 2001, 15:21:18)
[GCC 2.95.3-5 (cygwin special)] on cygwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> l = os.listdir('.')
>>> l
['compare.html', 'compare.py', 'compare.pyc', 'CVS', 'testCompare.py',
'xmlnode.
txt']
>>> for x in l:
...     print os.path.isfile(x)
...
1
1
1
0
1
1
>>> for x in l:
...     print os.path.isdir(x)
...
0
0
0
1
0
0
>>>





More information about the Python-list mailing list