Get the filenames in a directory and its subdirectories

P

Psymaster

That would be very handy for a small progrtam I've written. I've
browsed through the documentation for the standard modules but
there doesn't seem to be a way (please correct me if I'm wrong).

I know that it shouldn't be very hard to write a module myself
using listdir() and isdir() or stuff like that, but I'm having
problems.
 
F

Fredrik Lundh

Psymaster said:
That would be very handy for a small progrtam I've written. I've
browsed through the documentation for the standard modules but
there doesn't seem to be a way (please correct me if I'm wrong).

os.path.walk (with a rather weird interface; look it up in the docs)
I know that it shouldn't be very hard to write a module myself
using listdir() and isdir() or stuff like that, but I'm having problems.

here's a simple version; if you want, you can change it into
a generator (use yield to return the names, instead of adding
them to one big list), but it's perfectly usable as is [1]:

def listdir(root, path=""):
# recursive listdir
files = []
try:
for file in os.listdir(os.path.join(root, path)):
pathname = os.path.join(path, file)
if os.path.isdir(os.path.join(root, pathname)):
files.extend(listdir(root, pathname))
else:
files.append(pathname)
except OSError:
pass
return files

you may also want to take a look at the os.path examples in
my library book:

http://effbot.org/zone/librarybook-index.htm
=> core modules, page 1-35ff

</F>

1) unless I messed something up when posting it, of course.
 
E

Egor Bolonev

That would be very handy for a small progrtam I've written. I've
browsed through the documentation for the standard modules but
there doesn't seem to be a way (please correct me if I'm wrong).

I know that it shouldn't be very hard to write a module myself
using listdir() and isdir() or stuff like that, but I'm having
problems.

There are a my funcs (for Windows):


===========================================
def rec_glob(path,mask)
def rec_glob(mask)
===========================================
import os,fnmatch

def rec_glob_get_dirs(path):
try:
for i in os.listdir(path):
if os.path.isdir(path+i):
yield os.path.basename(i)
except:pass


def rec_glob(path,mask):
p=[]
if len(path)<>0:
if path[-1]!='\\':
path=path+'\\'
for i in rec_glob_get_dirs(path):
for ii in rec_glob(path+i,mask):
yield ii
try:
for i in os.listdir(path):
ii=i
i=path+i
if os.path.isfile(i):
if fnmatch.fnmatch(ii,mask):
yield i
except:pass


def rec_glob(mask):
rec_glob('',mask)



if __name__ == '__main__':
f=open('log','wb')
for i in rec_glob('E:','*'):
f.write(i+'\n')
f.close()


print 'Done.'
#while 1:pass
===========================================

and

===========================================
import os,fnmatch

def rec_glob_get_dirs(path):
d=[]
try:
for i in os.listdir(path):
if os.path.isdir(path+i):
d.append(os.path.basename(i))
except:pass
return d


def rec_glob(path,mask):
l=[]
if len(path)<>0:
if path[-1]!='\\':
path=path+'\\'
for i in rec_glob_get_dirs(path):
l=l+rec_glob(path+i,mask)
try:
for i in os.listdir(path):
ii=i
i=path+i
if os.path.isfile(i):
if fnmatch.fnmatch(ii,mask):
l.append(i)
except:pass
return l

def rec_glob(mask):
rec_glob('',mask)



if __name__ == '__main__':
f=open('log','wb')
for i in rec_glob('C:\\','*'):
f.write(i+'\n')
f.close()


print 'Done.'
#while 1:pass
===========================================
 
P

Psymaster

os.path.walk (with a rather weird interface; look it up in
the docs)

Yes, I've now understood ot, but it isn't only cryptic itself,
its documentation is even cryptier.

Anyway.
 

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

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top