Just want to walk a single directory

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • SB

    #1

    Just want to walk a single directory

    Hi,

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

    I thought this would do it, but "permanentFilel ist" 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"

    permanentFileli st= []

    for thepath,thedirn ames,thefilenam es in os.walk(thebase dir):

    if thepath != thebasedir:

    thedirnames[:] = []

    for names in thefilenames:

    permanentFileli st.append(names )
  • Tim Peters

    #2
    Re: Just want to walk a single directory

    [stevebnospam428 @hotmail.com][color=blue]
    > I have a super-simple need to just walk the files in a single directory.
    >
    > I thought this would do it, but "permanentFilel ist" ends up containing
    > all folders in all subdirectories.[/color]

    All folders everywhere, or all file (not directory) names in the top
    two levels? It looks like the latter to me.
    [color=blue]
    > 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"
    >
    > permanentFileli st= []
    >
    > for thepath,thedirn ames,thefilenam es in os.walk(thebase dir):
    >
    > if thepath != thebasedir:[/color]

    You wanted == instead of != there. Think about it ;-)
    [color=blue]
    > thedirnames[:] = []
    >
    > for names in thefilenames:
    > permanentFileli st.append(names )[/color]

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

    thebasedir = "C:\\tmpold "
    for dummy, dummy, permanentFileli st in os.walk(thebase dir):
    break

    or the possibly more cryptic equivalent:

    thebasedir = "C:\\tmpold "
    permanentFileli st = os.walk(thebase dir).next()[-1]

    or the wordier but transparent:

    thebasedir = "C:\\tmpold "
    permanentFileli st = [fn for fn in os.listdir(theb asedir)
    if os.path.isfile( os.path.join(th ebasedir, fn))]

    Comment

    • SB

      #3
      Re: Just want to walk a single directory

      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 <tim.peters@gma il.com> wrote in
      news:mailman.52 2.1137279686.27 775.python-list@python.org :
      [color=blue]
      > thebasedir = "C:\\tmpold "
      > for dummy, dummy, permanentFileli st in os.walk(thebase dir):
      > break[/color]

      Comment

      • Tim Roberts

        #4
        Re: Just want to walk a single directory

        SB <stevebnospam42 8@hotmail.com> wrote:
        [color=blue]
        >Hi,
        >
        >I have a super-simple need to just walk the files in a single directory.
        >
        >I thought this would do it, but "permanentFilel ist" 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"
        >
        >permanentFilel ist= []
        >
        >for thepath,thedirn ames,thefilenam es in os.walk(thebase dir):
        > if thepath != thebasedir:
        > thedirnames[:] = []
        > for names in thefilenames:
        > permanentFileli st.append(names )[/color]

        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, timr@probo.com
        Providenza & Boekelheide, Inc.

        Comment

        Working...