Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > shutil.move() Error ([Errno 17] File exists:) on win32

Reply
Thread Tools

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

 
 
bmgz
Guest
Posts: n/a
 
      12-16-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()..)


 
Reply With Quote
 
 
 
 
Hans Nowak
Guest
Posts: n/a
 
      12-16-2003
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 ((E-Mail Removed))
http://zephyrfalcon.org/



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
python-ldap/win32 or python/ldap/win32 rcmn Python 1 11-06-2006 11:47 PM
"Win32:Trojan-gen. {VC}" "Win32:Trojan-gen. {UPX!}" D@Z Computer Support 5 01-30-2006 07:52 PM
win32 process information, using win32 extension Java and Swing Python 1 10-24-2005 09:13 PM
RE: win32 process information, using win32 extension Tim Golden Python 0 10-21-2005 02:18 PM
Hmmm... problems with CPAN (MakeMaker?) on Win32+MSYS or Win32+UnixUtils and others Alex Lyman Perl 0 03-07-2004 05:10 PM



Advertisments