473,378 Members | 1,482 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

os.walk Value Error?

Hi,

I'm using os.walk as follows:

(basedir, pathnames, files) = os.walk("results", 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?
Jun 27 '08 #1
4 3666
td****@gmail.com wrote:
Hi,

I'm using os.walk as follows:

(basedir, pathnames, files) = os.walk("results", 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("results"):
#
# Do you work inside the loop
#

-Larry
Jun 27 '08 #2
td****@gmail.com 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

Jun 27 '08 #3
On Jun 14, 7:11*pm, Larry Bates <larry.ba...@websafe.com`wrote:
tda...@gmail.com wrote:
Hi,
I'm using os.walk as follows:
(basedir, pathnames, files) = os.walk("results", 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("results"):
* * *#
* * *# Do you work inside the loop
* * *#

-Larry
Thanks!
Jun 27 '08 #4
En Sat, 14 Jun 2008 19:06:16 -0300, <td****@gmail.comescribió:
I'm using os.walk as follows:

(basedir, pathnames, files) = os.walk("results", 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

Jun 27 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: hokieghal99 | last post by:
This script is not recursive... in order to make it recursive, I have to call it several times (my kludge... hey, it works). I thought os.walk's sole purpose was to recursively walk a directory...
7
by: rbt | last post by:
This function is intended to remove unwanted files and dirs from os.walk(). It will return correctly *IF* I leave the 'for fs in fs_objects' statement out (basically leave out the entire purpose of...
8
by: fuzzylollipop | last post by:
I am trying to get the number of bytes used by files in a directory. I am using a large directory ( lots of stuff checked out of multiple large cvs repositories ) and there is lots of wasted time...
7
by: pembed2003 | last post by:
Hi, I have a question about how to walk a binary tree. Suppose that I have this binary tree: 8 / \ 5 16 / \ / \ 3 7 9 22 / \ / \ / \
7
by: KraftDiner | last post by:
The os.walk function walks the operating systems directory tree. This seems to work, but I don't quite understand the tupple that is returned... Can someone explain please? for root, dirs,...
9
by: silverburgh.meryl | last post by:
i am trying to use python to walk thru each subdirectory from a top directory. Here is my script: savedPagesDirectory = "/home/meryl/saved_pages/data" dir=open(savedPagesDirectory, 'r') ...
4
by: Marcus Alves Grando | last post by:
Hello list, I have a strange problem with os.walk and threads in python script. I have one script that create some threads and consume Queue. For every value in Queue this script run os.walk()...
2
by: | last post by:
Hi everyone The following code: scriptPath = os.path.dirname(__file__) (dirpath, dirnames, filenames) = os.walk(scriptPath) print 'dirpath\n' print dirpath print 'dirnames\n'...
0
by: Jeff McNeil | last post by:
Your args are fine, that's just the way os.path.walk works. If you just need the absolute pathname of a directory when given a relative path, you can always use os.path.abspath, too. A couple...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.