[Tutor] getting around ValueError in os.walk

Jason Massey jason.massey at gmail.com
Mon Jan 30 23:34:53 CET 2006


I put in your code, exactly as you have it, with only three changes:

import os

for (dirpath, subdirs, filenames) in os.walk("/python24"):    # a comma
instead of a period after dirpath
       for file in filenames:
               if file.endswith(".py"):
# py instead of baz, just for my machine
                       print os.path.join(dirpath,file)                   #
file instead of f

This walks the directories and prints out the path and filename.

On a side note I've never had a problem throwing several thousand files or
lines at python.

On 1/30/06, Andrew D. Fant <fant at pobox.com> wrote:
> I'm working on a program to do some processing on a directory tree. If I
> had
> been doing it in a shell script, the core of the processing would have
> been in
> a "find $ROOT -type f -name FOO -print" command"
> On the web, I found a snippet of code that demonstrated the os.walk module
> and
> I created a simple test program to work on my tree that looked like this:
> #!/usr/bin/python
> import os
> for (dirpath. subdirs, filenames) in os.walk("/foo/bar"):
>         for file in filenames:
>                 if file.endswith(".baz"):
>                         print os.path.join(dirpath,f)
> when I try to run it, I get a "ValueError: too many values to unpack"
> which I
> think  comes from the fact that there is a subdirectory of /foo/bar which
> has
> over 2500 files in it.  The tree can't be easily restructured for legacy
> reasons.  Can anyone suggest the best way to get around this in code?
> Thanks,
>         Andy
> --
> Andrew Fant    | And when the night is cloudy    | This space to let
> Molecular Geek | There is still a light          |----------------------
> fant at pobox.com | That shines on me               | Disclaimer:  I don't
> Boston, MA     | Shine until tomorrow, Let it be | even speak for myself
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20060130/11ede0da/attachment.html 

More information about the Tutor mailing list