[Tutor] Move all files to top-level directory

Dave Angel davea at ieee.org
Mon Apr 12 22:13:24 CEST 2010

Dotan Cohen wrote:
> All right, I have gotten quite a bit closer, but Python is now
> complaining about the directory not being empty:
> ✈dcl:test$ cat moveUp.py
> #!/usr/bin/python
> # -*- coding: utf-8 -*-
> import os
> currentDir =s.getcwd()
> filesList =s.walk(currentDir)
> for root, folder, file in filesList:
Why is the print below commented out?
>     for f in file:
>         toMove =oot + "/" + f
>         #print toMove
>         os.rename(toMove, currentDir)
> ✈dcl:test$ ./moveUp.py
> Traceback (most recent call last):
>   File "./moveUp.py", line 11, in <module>
>     os.rename(toMove, currentDir)
> OSError: [Errno 39] Directory not empty
> I am aware that the directory is not empty, nor should it be! How can
> I override this?
> Thanks!
Have you looked at the value of "currentDir" ? Is it in a form that's 
acceptible to os.rename() ? And how about toMove? Perhaps it has two 
slashes in a row in it. When combining directory paths, it's generally 
safer to use


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 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."


More information about the Tutor mailing list