classification
Title: shutil.rmtree with empty filepath
Type: behavior Stage: resolved
Components: Windows Versions: Python 2.6
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: tarek Nosy List: brian.curtin, dbkoch, r.david.murray, tarek, ysj.ray
Priority: low Keywords:

Created on 2010-05-13 16:57 by dbkoch, last changed 2010-11-20 12:31 by r.david.murray. This issue is now closed.

Messages (6)
msg105646 - (view) Author: Dan Koch (dbkoch) Date: 2010-05-13 16:57
The following sequence raises an exception, but nonetheless removes all files from the Desktop under Windows Vista.

import os, shutil, user

desktop_dir = os.path.join(user.home, 'Desktop')
os.chdir(desktop_dir)

shutil.rmtree('')

This does not occur under Fedora 12. Not tested on Mac OS X yet.
msg105686 - (view) Author: ysj.ray (ysj.ray) Date: 2010-05-14 06:40
Tested on Debian 5, also not occurs.
msg105695 - (view) Author: Tarek Ziadé (tarek) * (Python committer) Date: 2010-05-14 10:04
Dan,

Can I have the error you get ?

Thanks
msg105716 - (view) Author: Dan Koch (dbkoch) Date: 2010-05-14 14:58
Here's the session printout. Desktop files under Vista still get deleted despite the exception. Does not occur on Fedora 12 or Mac OS X. I can code around it by testing for a blank filepath, but it was a surprise.

C:\Users\ko5>python
Python 2.6.4 (r264:75708, Oct 26 2009, 08:23:19) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import os, shutil, user
>>> desktop_dir = os.path.join(user.home, 'Desktop')
>>> os.chdir(desktop_dir)
>>> os.getcwd()
'C:\\Users\\ko5\\Desktop'
>>> shutil.rmtree('')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python26\lib\shutil.py", line 225, in rmtree
    onerror(os.rmdir, path, sys.exc_info())
  File "C:\Python26\lib\shutil.py", line 223, in rmtree
    os.rmdir(path)
WindowsError: [Error 3] The system cannot find the path specified: ''
>>>
msg121595 - (view) Author: Brian Curtin (brian.curtin) * (Python committer) Date: 2010-11-20 05:18
This can't actually work. You can't delete a directory which has open handles to it on Windows, namely the Python process you're running in that directory.

The empty file path isn't really the issue here. shutil.rmtree(os.getcwd()) attempts the same thing but gives you a better error message since a full path gets sent down to os.rmdir rather than "".
msg121621 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2010-11-20 12:31
Just as an FYI, a similar situation exists on Solaris.  I had to fix one of the Python test suite tests once because it was naively trying to rmtree the CWD.
History
Date User Action Args
2010-11-20 12:31:20r.david.murraysetnosy: + r.david.murray
messages: + msg121621
2010-11-20 05:18:15brian.curtinsetstatus: open -> closed

nosy: + brian.curtin
messages: + msg121595

resolution: rejected
stage: resolved
2010-05-14 14:58:46dbkochsetmessages: + msg105716
2010-05-14 10:04:35tareksetmessages: + msg105695
2010-05-14 10:03:59tareksetpriority: normal -> low
assignee: tarek
2010-05-14 06:40:56ysj.raysetnosy: + ysj.ray
messages: + msg105686
2010-05-14 05:01:02r.david.murraysetnosy: + tarek
2010-05-13 16:57:23dbkochcreate