os.path.walk() to get full path of all files

Discussion in 'Python' started by dude, Mar 16, 2011.

  1. dude

    dude Guest

    My goal is create a list of absolute paths for all files in a given
    directory (any number of levels deep).

    root
    ----dir1
    --------file1
    --------file2
    --------dir2
    ------------file3
    --------dir3
    -------------dir4
    ------------------file4
    ----file5

    So the above would return:
    [root/dir1/file1, root/dir1/file2, root/dir1/dir2/file3, etc...]

    I've been trying different ways of using os.path.walk() for that, but
    I can't find an elegant way. Anyone know of something simple to
    accomplish that?
     
    dude, Mar 16, 2011
    #1
    1. Advertising

  2. On 16.03.2011 20:41, dude wrote:
    > My goal is create a list of absolute paths for all files in a given
    > directory (any number of levels deep).
    >
    > root
    > ----dir1
    > --------file1
    > --------file2
    > --------dir2
    > ------------file3
    > --------dir3
    > -------------dir4
    > ------------------file4
    > ----file5
    >
    > So the above would return:
    > [root/dir1/file1, root/dir1/file2, root/dir1/dir2/file3, etc...]
    >
    > I've been trying different ways of using os.path.walk() for that, but
    > I can't find an elegant way. Anyone know of something simple to
    > accomplish that?


    Try this:

    file_list = []
    for root, _, filenames in os.walk(root_path):
    for filename in filenames:
    file_list.append(os.path.join(root, filename))

    for x in file_list:
    print x
     
    Alexander Kapps, Mar 16, 2011
    #2
    1. Advertising

  3. On Wed, Mar 16, 2011 at 5:00 PM, dude <> wrote:
    > awesome, that worked.  I'm not sure how the magic is working with your underscores there, but it's doing what I need.  thanks.
    > --


    It's not magic at all. _ is just a variable name. When someone names a
    variable _, it's just to let you know that they won't actually be
    using that variable. In this case, os.walk returns three things: the
    root, the list of directories, and the list of files. You only need
    two of those for your code, so you ignore the third.
     
    Benjamin Kaplan, Mar 16, 2011
    #3

  4. > file_list = []
    > for root, _, filenames in os.walk(root_path):
    > for filename in filenames:
    > file_list.append(os.path.join(root, filename))



    What does the notation "_" stands for ? Is it a sort of /dev/null ?

    I know that in the terminal it represents the last printed text.

    Laurent
     
    Laurent Claessens, Mar 17, 2011
    #4
  5. dude

    Tim Golden Guest

    On 17/03/2011 08:58, Laurent Claessens wrote:
    >
    >> file_list = []
    >> for root, _, filenames in os.walk(root_path):
    >> for filename in filenames:
    >> file_list.append(os.path.join(root, filename))

    >
    >
    > What does the notation "_" stands for ? Is it a sort of /dev/null ?
    >
    > I know that in the terminal it represents the last printed text.


    It's a convention for saying "I don't really care about this
    particular value which is returned from that function". You
    could, at your whim, use "dontcare" or "x" or whatever.

    TJG
     
    Tim Golden, Mar 17, 2011
    #5
  6. Laurent Claessens writes:

    > > file_list = []
    > > for root, _, filenames in os.walk(root_path):
    > > for filename in filenames:
    > > file_list.append(os.path.join(root, filename))

    >
    > What does the notation "_" stands for ? Is it a sort of /dev/null ?


    >>> x, _, y = 1, "hukairs", 3
    >>> x, y

    (1, 3)
    >>> _

    'hukairs'
    >>>
     
    Jussi Piitulainen, Mar 17, 2011
    #6
    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. Florian Lindner

    os.walk: Get entire path

    Florian Lindner, Aug 29, 2004, in forum: Python
    Replies:
    4
    Views:
    461
    Miki Tebeka
    Aug 30, 2004
  2. xtian

    Re: os.walk: Get entire path

    xtian, Aug 29, 2004, in forum: Python
    Replies:
    0
    Views:
    407
    xtian
    Aug 29, 2004
  3. Marcus Alves Grando
    Replies:
    7
    Views:
    479
    Marcus Alves Grando
    Nov 14, 2007
  4. albert kao
    Replies:
    3
    Views:
    625
    Tim Chase
    May 14, 2010
  5. Replies:
    3
    Views:
    243
    Öö Tiib
    Feb 1, 2013
Loading...

Share This Page