shutil.move() Error ([Errno 17] File exists:) on win32

bmgz bmgz at dev.null
Tue Dec 16 10:34:19 CET 2003


I am have made a simple script that moves all desktop clutter (ie files that
are not *.lnk) to a specified folder
eg. c:\myhome\mydocs\desktopdebris\2003-12-16

----------------------------------------------------------------------------
-----------------
import re, os, time, shutil
os.chdir(os.environ['HOMEDRIVE']+os.environ['HOMEPATH']+"\\Desktop")
DESKTOP =
os.listdir(os.environ['HOMEDRIVE']+os.environ['HOMEPATH']+"\\Desktop")
TODAYDIR = "E:\\My Documents\\desktop_debris\\"+time.strftime('%Y-%m-%d')


if not os.path.exists(TODAYDIR):
    os.mkdir(TODAYDIR)

for i in range(len(DESKTOP)):
    if not re.search("\.lnk$", DESKTOP[i]):
        shutil.move(DESKTOP[i],TODAYDIR)
----------------------------------------------------------------------------
-----------------------

this works fine for FILES on the desktop, but when a FOLDER is present the
script exits with the following error:

Traceback (most recent call last):
  File "E:\My Documents\python\cleanup_desktop.py", line 12, in ?
    shutil.move(DESKTOP[i],TODAYDIR)
  File "C:\Program Files\Python-2.3\lib\shutil.py", line 167, in move
    copytree(src, dst, symlinks=True)
  File "C:\Program Files\Python-2.3\lib\shutil.py", line 101, in copytree
    os.mkdir(dst)
OSError: [Errno 17] File exists: 'E:\\My
Documents\\desktop_debris\\2003-12-16'


It seems that the shutil.move() function  is trying to recreate the target
directory? Is their another module one could use in this case?
(shutil.copy() then delete() also gives problems - then again that IS
move()..)






More information about the Python-list mailing list