os.path.walk() to get full path of all files
Alexander Kapps
alex.kapps at web.de
Wed Mar 16 16:05:46 EDT 2011
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
More information about the Python-list
mailing list