Get the filenames in a directory and its subdirectories

Discussion in 'Python' started by Psymaster, Jun 29, 2003.

  1. Psymaster

    Psymaster Guest

    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.
     
    Psymaster, Jun 29, 2003
    #1
    1. Advertising

  2. "Psymaster" wrote:

    > 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.
     
    Fredrik Lundh, Jun 29, 2003
    #2
    1. Advertising

  3. Psymaster

    Egor Bolonev Guest

    > 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
    ===========================================
     
    Egor Bolonev, Jun 29, 2003
    #3
  4. Psymaster

    Psymaster Guest

    "Fredrik Lundh" <> wrote on Sun, 29 Jun
    2003 21:23:13 GMT in comp.lang.python:

    >> 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)
    >


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

    Anyway.
     
    Psymaster, Jun 30, 2003
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. B.J.
    Replies:
    4
    Views:
    772
    Toby Inkster
    Apr 23, 2005
  2. thunk
    Replies:
    1
    Views:
    359
    thunk
    Mar 30, 2010
  3. thunk
    Replies:
    0
    Views:
    536
    thunk
    Apr 1, 2010
  4. thunk
    Replies:
    14
    Views:
    659
    thunk
    Apr 3, 2010
  5. lucy
    Replies:
    6
    Views:
    175
    Michele Dondi
    Sep 3, 2004
Loading...

Share This Page