[Tutor] Move all files to top-level directory
Wayne Werner
waynejwerner at gmail.com
Mon Apr 12 20:53:27 CEST 2010
On Mon, Apr 12, 2010 at 1:21 PM, Dotan Cohen <dotancohen at gmail.com> 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 = os.getcwd()
>
> filesList = os.walk(currentDir)
> for root, folder, file in filesList:
> for f in file:
> toMove = root + "/" + 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?
>
from the docs:
os.rename(*src*, *dst*)¶
<http://docs.python.org/library/os.html#os.rename>Rename
the file or directory *src* to *dst*. If *dst* is a directory,
OSError<http://docs.python.org/library/exceptions.html#exceptions.OSError>will
be raised. On Unix, if
*dst* exists and is a file, it will be replaced silently if the user has
permission. The operation may fail on some Unix flavors if *src* and
*dst*are on different filesystems. If successful, the renaming will be
an atomic
operation (this is a POSIX requirement). On Windows, if *dst* already
exists, OSError<http://docs.python.org/library/exceptions.html#exceptions.OSError>will
be raised even if it is a file; there may be no way to implement an
atomic rename when *dst* names an existing file. Availability: Unix,
Windows.It seems what you wan to do is os.rename(toMove, currentDir+f)
HTH,
Wayne
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100412/54734416/attachment.html>
More information about the Tutor
mailing list