471,620 Members | 1,184 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,620 software developers and data experts.

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

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()..)
Jul 18 '05 #1
1 7274
bmgz wrote:
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'


I'm not sure, but I think it kinda works like this.

When you move a file, you can do:

shutil.move(filename, directory)

e.g.

shutil.move('test.txt', 'c:/temp')

This moves the file to the c:/temp directory. Nothing surprising here.

However, when moving a directory, this is a bit different. shutil.move(dir1,
dir2) doesn't work if dir2 already exists. Rather, you need to specify the
*new name* of the directory:

shutil.move('c:/dir1', 'c:/temp')
# WRONG if c:/temp already exists

shutil.move('c:/dir1', 'c:/temp/dir1')
# does what you want

HTH,

--
Hans (ha**@zephyrfalcon.org)
http://zephyrfalcon.org/

Jul 18 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Stéphane Ninin | last post: by
6 posts views Thread by Daniel Bickett | last post: by
reply views Thread by Morten Gulbrandsen | last post: by
1 post views Thread by Todd7 | last post: by
6 posts views Thread by Antoine De Groote | last post: by
3 posts views Thread by Horse | last post: by
4 posts views Thread by Roopesh | last post: by
1 post views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.