Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

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

8,586 views
Skip to first unread message

dude

unread,
Mar 16, 2011, 3:41:30 PM3/16/11
to
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?

Alexander Kapps

unread,
Mar 16, 2011, 4:05:46 PM3/16/11
to pytho...@python.org

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

dude

unread,
Mar 16, 2011, 5:00:42 PM3/16/11
to pytho...@python.org
awesome, that worked. I'm not sure how the magic is working with your underscores there, but it's doing what I need. thanks.

dude

unread,
Mar 16, 2011, 5:00:42 PM3/16/11
to comp.lan...@googlegroups.com, pytho...@python.org

Benjamin Kaplan

unread,
Mar 16, 2011, 5:13:22 PM3/16/11
to pytho...@python.org
On Wed, Mar 16, 2011 at 5:00 PM, dude <erni...@gmail.com> 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.

Alexander Kapps

unread,
Mar 16, 2011, 5:15:32 PM3/16/11
to pytho...@python.org
On 16.03.2011 22:00, 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.

The underscore is no magic here. It's just a conventional variable
name, saying "I m unused". One could also write:

for root, UNUSED, filenames in os.walk(root_path):
...


BTW. Please quote enough of the posts you reply to. Most people
access this list/newsgroup per mail client or usenet reader, not per
a webpage, so without quoting, they might not see the context of our
post. Thank you.

Laurent Claessens

unread,
Mar 17, 2011, 4:58:22 AM3/17/11
to

> 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

Tim Golden

unread,
Mar 17, 2011, 5:08:10 AM3/17/11
to pytho...@python.org

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

Jussi Piitulainen

unread,
Mar 17, 2011, 5:09:07 AM3/17/11
to
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'
>>>

0 new messages