How can I get the names of the files in a directory?

E

Egor Bolonev

Sara Fwd said the following on 1/15/2005 8:10 AM:

You can use glob:


(will print a list of all files in the given directory, matching the
given pattern)

If you want to traverse a directory tree recursively, please take a look
at this recipe:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/200131

import os, os.path

def get_all_files(path):
if len(path) > 0:
if path[-1] == ':':
path = path + '\\'
try:
for i in os.listdir(path):
j = os.path.join(path, i)
if os.path.isdir(j):
for ii in get_all_files(j):
yield ii
else:
yield j
except:pass

for i in get_all_files('c:\\'):
print i
 
S

Stian Soiland

På 15. jan 2005 kl. 16:16 skrev .removethis.:

Nice example of when filter() is better than list comprehension.

[f for f in glob.glob("/tmp/*") if isfile(fi)]
is a bit too verbose, the iteration is a totally uninteresting part
here.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top