How do I get a list of all files (and directories) in a given directory in Python?
+24
A:
You can use os.listdir(path). See more os functions here: http://docs.python.org/lib/os-file-dir.html
rslite
2008-09-23 12:32:00
Pass it a Unicode string to get Unicode return value.
Craig McQueen
2009-09-05 22:28:47
+1
A:
Try this:
import os
for top, dirs, files in os.walk('./'):
for nm in files:
print os.path.join(top, nm)
paxdiablo
2008-09-23 12:34:34
+26
A:
This is a way to traverse every file and directory in a directory tree:
import os
for dirname, dirnames, filenames in os.walk('.'):
for subdirname in dirnames:
print os.path.join(dirname, subdirname)
for filename in filenames:
print os.path.join(dirname, filename)
Jerub
2008-09-23 12:35:46