473,378 Members | 1,621 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 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 7711
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Stéphane Ninin | last post by:
Hello world, I am fighting with what is probably a stupid problem. In a wxpython GUI, here is a method which must read a file, and if the file is not correctly formed rename it and create a...
6
by: Daniel Bickett | last post by:
Hello, I'm writing an application in my pastime that moves files around to achieve various ends -- the specifics aren't particularly important. The shutil module was chosen as the means simply...
0
by: Morten Gulbrandsen | last post by:
mysql> USE company; Database changed mysql> mysql> DROP TABLE IF EXISTS EMPLOYEE; -------------- DROP TABLE IF EXISTS EMPLOYEE -------------- Query OK, 0 rows affected (0.00 sec)
1
by: Todd7 | last post by:
I am writing a python program to load a pdf file into an IEHtmlWindow which displays it through adobe acrobat reader 7. Depending on the buttons the user clicks, the program moves it to another...
8
by: Claudio Grondi | last post by:
Here an example of what I mean (Python 2.4.2, IDLE 1.1.2, Windows XP SP2, NTFS file system, 80 GByte large file): Traceback (most recent call last): File "<pyshell#1>", line 1, in -toplevel-...
6
by: Antoine De Groote | last post by:
Google tells quite some things about it, but none of them are satisfactory. I'm on Windows, and shutil operations (e.g. move, copy) throw Permission denied all the time, for the source files. It...
3
by: Horse | last post by:
I've written a python script that copies a nightly Oracle backup file to another server. Every couple days, the script fails with this error message: Error copying...
4
by: Roopesh | last post by:
Hi, I have a multithreaded application. There are two threads, T1 and T2. Suppose that there are two folders A, B. Thread T1 fetches data from network and creates files in folder A and after...
4
by: klia | last post by:
hello folks i am trying to tweak the current codes so that later when i call it from the terminal i can provide sourcefile and the destination file rather being fixed in the code. because now i...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.