[Tutor] Move all files to top-level directory
Dave Angel
davea at ieee.org
Mon Apr 12 21:46:45 CEST 2010
Dotan Cohen wrote:
> On 12 April 2010 20:12, Sander Sweers <sander.sweers at gmail.com> wrote:
>
>> On 12 April 2010 18:28, Dotan Cohen <dotancohen at gmail.com> wrote:
>>
>>> However, it fails like this:
>>> $ ./moveUp.py
>>> Traceback (most recent call last):
>>> File "./moveUp.py", line 8, in <module>
>>> os.rename(f, currentDir)
>>> TypeError: coercing to Unicode: need string or buffer, tuple found
>>>
>> os.rename needs the oldname and the new name of the file. os.walk
>> returns a tuple with 3 values and it errors out.
>>
>>
>
> I see, thanks. So I was sending it four values apparently. I did not
> understand the error message.
>
>
No, you're sending it two values: a tuple, and a string. It wants two
strings. Thus the error. If you had sent it four values, you'd have
gotten a different error.
> <snip>
> Actually, I will add a check that cwd !=HOME || $HOME/.bin as those
> are the only likely places it might run by accident. Or maybe I'll
> wrap it in Qt and add a confirm button.
>
>
>
>> os.walk returns you a tuple with the following values:
>> (the root folder, the folders in the root, the files in the root folder).
>>
>> You can use tuple unpacking to split each one in separate values for
>> your loop. Like:
>>
>> for root, folder, files in os.walk('your path):
>> #do stuff
>>
>>
>
> I did see that while googling, but did not understand it. Nice!
>
>
>
Judging from your next message, you still don't understand it.
>> It might be wise to only have this module print what it would do
>> instead of doing the actual move/rename so you can work out the bugs
>> first before it destroys your data.
>>
>>
>
> I am testing on fake data, naturally.
>
>
Is your entire file system fake? Perhaps you're running in a VM, and
don't mind trashing it.
While debugging, you're much better off using prints than really moving
files around. You might be amazed how much damage a couple of minor
bugs could cause.
DaveA
More information about the Tutor
mailing list