[Tutor] Move all files to top-level directory

Dotan Cohen dotancohen at gmail.com
Tue Apr 13 09:29:37 CEST 2010

> Why is the print below commented out?
>>    for f in file:
>>        toMove =oot + "/" + f
>>        #print toMove
>>        os.rename(toMove, currentDir)

I was testing, and it was not longer needed. It was returning what I
expected. Note that print can only be used to test that the value
matches what the dev expects, not to test that the value matches what
the function wants as we will soon see in the case of currentDir.

> Have you looked at the value of "currentDir" ? Is it in a form that's
> acceptible to os.rename() ?

No, but I though that it was. I had assumed that os.rename would
automatically add the filename as the unix command mv does. Yes, I
know what assumptions make out of me!

> And how about toMove? Perhaps it has two slashes
> in a row in it.

No, toMove was fine.

> When combining directory paths, it's generally safer to use
> os.path.join()


> Next, you make no check whether "root" is the same as "currentDir". So if
> there are any files already in the top-level directory, you're trying to
> rename them to themselves.

I have no problem with that. It is unnecessary, but not harmful at
this stage. But thank you for mentioning it, I will get to that.

> I would also point out that your variable names are very confusing. "file"
> is a list of files, so why isn't it plural? Likewise "folders."

Obfuscation! Just kidding, I did not realize that I would be
interacting with "file" as a list of files and not as an individual
file at the time I wrote that. I will revise the variable names, that
is good practice.

Thank you for your patience and advice. I am really enjoying Python
now that I am starting to get the hang of it. I do wish that the docs
had more usage examples but the help on this list and the volume of
information available through google mostly negates that. Thank you
very much.

Dotan Cohen


More information about the Tutor mailing list