os.walk Value Error?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • tdahsu@gmail.com

    os.walk Value Error?

    Hi,

    I'm using os.walk as follows:

    (basedir, pathnames, files) = os.walk("result s", topdown=True)

    and I'm getting the error:

    ValueError: too many values to unpack

    From my googling, that means:

    This is the standard message when Python tries to unpack a tuple
    into fewer variables than are in the tuple.

    From what I can see of the examples on the python site, I'm using it
    correctly. I have commas in my original code, and the "results"
    directory exists and is directly under the directory from which my
    script is run.

    I'm assuming that 12 files (the number of files in the "results"
    directory) is not too many for Python to handle! ;-)

    Is there any other reason I might get that error?
  • Larry Bates

    #2
    Re: os.walk Value Error?

    tdahsu@gmail.co m wrote:
    Hi,
    >
    I'm using os.walk as follows:
    >
    (basedir, pathnames, files) = os.walk("result s", topdown=True)
    >
    and I'm getting the error:
    >
    ValueError: too many values to unpack
    >
    From my googling, that means:
    >
    This is the standard message when Python tries to unpack a tuple
    into fewer variables than are in the tuple.
    >
    From what I can see of the examples on the python site, I'm using it
    correctly. I have commas in my original code, and the "results"
    directory exists and is directly under the directory from which my
    script is run.
    >
    I'm assuming that 12 files (the number of files in the "results"
    directory) is not too many for Python to handle! ;-)
    >
    Is there any other reason I might get that error?
    os.walk is a generator so you need to make it a loop target:

    for basedir, pathnames, files in os.walk("result s"):
    #
    # Do you work inside the loop
    #

    -Larry

    Comment

    • Christian Heimes

      #3
      Re: os.walk Value Error?

      tdahsu@gmail.co m wrote:
      Is there any other reason I might get that error?
      Yes, you are using it the wrong way. The correct way is

      for root, dirs, files in os.walk(path):
      do something

      os.walk returns an iterator which yields root, dirs and files for each
      iteration.

      Christian

      Comment

      • tdahsu@gmail.com

        #4
        Re: os.walk Value Error?

        On Jun 14, 7:11 pm, Larry Bates <larry.ba...@we bsafe.com`wrote :
        tda...@gmail.co m wrote:
        Hi,
        >
        I'm using os.walk as follows:
        >
        (basedir, pathnames, files) = os.walk("result s", topdown=True)
        >
        and I'm getting the error:
        >
        ValueError: too many values to unpack
        >
        From my googling, that means:
        >
        This is the standard message when Python tries to unpack a tuple
        into fewer variables than are in the tuple.
        >
        From what I can see of the examples on the python site, I'm using it
        correctly.  I have commas in my original code, and the "results"
        directory exists and is directly under the directory from which my
        script is run.
        >
        I'm assuming that 12 files (the number of files in the "results"
        directory) is not too many for Python to handle!  ;-)
        >
        Is there any other reason I might get that error?
        >
        os.walk is a generator so you need to make it a loop target:
        >
        for basedir, pathnames, files in os.walk("result s"):
             #
             # Do you work inside the loop
             #
        >
        -Larry
        Thanks!

        Comment

        • Gabriel Genellina

          #5
          Re: os.walk Value Error?

          En Sat, 14 Jun 2008 19:06:16 -0300, <tdahsu@gmail.c omescribió:
          I'm using os.walk as follows:
          >
          (basedir, pathnames, files) = os.walk("result s", topdown=True)
          >
          and I'm getting the error:
          >
          ValueError: too many values to unpack
          >
          >From my googling, that means:
          >
          This is the standard message when Python tries to unpack a tuple
          into fewer variables than are in the tuple.
          >
          >From what I can see of the examples on the python site, I'm using it
          correctly. I have commas in my original code, and the "results"
          directory exists and is directly under the directory from which my
          script is run.
          Look the examples more carefully again - they don't use an assignment, but another Python statement...

          --
          Gabriel Genellina

          Comment

          Working...