[Tutor] getting around ValueError in os.walk
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:
for (dirpath, subdirs, filenames) in os.walk("/python24"): # a comma
instead of a period after dirpath
for file in filenames:
# 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
> 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
> I created a simple test program to work on my tree that looked like this:
> 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
> 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?
> 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
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Tutor