Just want to walk a single directory

Discussion in 'Python' started by SB, Jan 14, 2006.

  1. SB

    SB Guest

    Hi,

    I have a super-simple need to just walk the files in a single directory.

    I thought this would do it, but "permanentFilelist" ends up containing
    all folders in all subdirectories.

    Could someone spot the problem? I've scoured some threads using XNews reg
    expressions involving os.walk, but couldn't extrapolate the answer for my
    need.

    ===============================================

    thebasedir = "E:\\temp"

    permanentFilelist= []

    for thepath,thedirnames,thefilenames in os.walk(thebasedir):

    if thepath != thebasedir:

    thedirnames[:] = []

    for names in thefilenames:

    permanentFilelist.append(names)
    SB, Jan 14, 2006
    #1
    1. Advertising

  2. SB

    Tim Peters Guest

    []
    > I have a super-simple need to just walk the files in a single directory.
    >
    > I thought this would do it, but "permanentFilelist" ends up containing
    > all folders in all subdirectories.


    All folders everywhere, or all file (not directory) names in the top
    two levels? It looks like the latter to me.

    > Could someone spot the problem? I've scoured some threads using XNews reg
    > expressions involving os.walk, but couldn't extrapolate the answer for my
    > need.
    >
    > ===============================================
    >
    > thebasedir = "E:\\temp"
    >
    > permanentFilelist= []
    >
    > for thepath,thedirnames,thefilenames in os.walk(thebasedir):
    >
    > if thepath != thebasedir:


    You wanted == instead of != there. Think about it ;-)

    > thedirnames[:] = []
    >
    > for names in thefilenames:
    > permanentFilelist.append(names)


    A simpler way (assuming I understand what you're after) is:

    thebasedir = "C:\\tmpold"
    for dummy, dummy, permanentFilelist in os.walk(thebasedir):
    break

    or the possibly more cryptic equivalent:

    thebasedir = "C:\\tmpold"
    permanentFilelist = os.walk(thebasedir).next()[-1]

    or the wordier but transparent:

    thebasedir = "C:\\tmpold"
    permanentFilelist = [fn for fn in os.listdir(thebasedir)
    if os.path.isfile(os.path.join(thebasedir, fn))]
    Tim Peters, Jan 14, 2006
    #2
    1. Advertising

  3. SB

    SB Guest

    Thanks Tim, this definitely does it. I was trying to prevent having to
    spend time absorbing the core of how generators work, because this simple
    is all I need to do, and I don't have the updated python cookbook book. The
    one I have discussed the old os.path.walk.



    Tim Peters <> wrote in
    news::

    > thebasedir = "C:\\tmpold"
    > for dummy, dummy, permanentFilelist in os.walk(thebasedir):
    > break
    SB, Jan 14, 2006
    #3
  4. SB

    Tim Roberts Guest

    SB <> wrote:

    >Hi,
    >
    >I have a super-simple need to just walk the files in a single directory.
    >
    >I thought this would do it, but "permanentFilelist" ends up containing
    >all folders in all subdirectories.
    >
    >Could someone spot the problem? I've scoured some threads using XNews reg
    >expressions involving os.walk, but couldn't extrapolate the answer for my
    >need.
    >
    >===============================================
    >
    >thebasedir = "E:\\temp"
    >
    >permanentFilelist= []
    >
    >for thepath,thedirnames,thefilenames in os.walk(thebasedir):
    > if thepath != thebasedir:
    > thedirnames[:] = []
    > for names in thefilenames:
    > permanentFilelist.append(names)


    Um, excuse me for butting in, but couldn't you accomplish the same thing
    more simply by using os.listdir and os.path.isfile? In my brain, os.walk
    is the solution to RECURSIVE search needs.
    --
    - Tim Roberts,
    Providenza & Boekelheide, Inc.
    Tim Roberts, Jan 15, 2006
    #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. SD
    Replies:
    1
    Views:
    433
  2. Chris
    Replies:
    1
    Views:
    13,601
    Oisin
    Mar 24, 2006
  3. Replies:
    6
    Views:
    30,229
    Diplomat
    Jan 12, 2011
  4. Marcus Alves Grando
    Replies:
    7
    Views:
    453
    Marcus Alves Grando
    Nov 14, 2007
  5. Replies:
    4
    Views:
    137
    Ted Zlatanov
    Sep 13, 2006
Loading...

Share This Page