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

D

dude

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?
 
A

Alexander Kapps

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
 
B

Benjamin Kaplan

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.
 
L

Laurent Claessens

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
 
T

Tim Golden

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
 
J

Jussi Piitulainen

Laurent said:
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 ?
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,965
Messages
2,570,148
Members
46,710
Latest member
FredricRen

Latest Threads

Top