[Python-ideas] Adding `pathlib.Path` method that would send file to recycle bin

Steven D'Aprano steve at pearwood.info
Thu Jan 1 13:35:26 CET 2015


On Thu, Jan 01, 2015 at 12:37:55PM +0200, Ram Rachum wrote:

> So maybe we can add a function to the `os` module that sends a file to the
> recycle bin, and a constant that will say whether the current OS supports
> this? Then we could have code like this:

The os module is for low-level operating-system functions. "Send to 
trash" is neither low-level nor part of the OS per se, it is part of the 
desktop environment.

I'm not convinced that this needs to be in the standard library, but if 
it is, I think that the os module is completely the wrong place for it. 
I think, in order of preference:

1) shutil
2) pathlib

is the right place. 

(The actual implementation for the move_to_trash function could come 
from another, platform-specific, module.)


> if os.RECYCLE_BIN_SUPPORT:
>     os.recycle(my_file)
> else:
>     os.remove(my_file)


"Recycle" is not a good name for the function, because it doesn't 
recycle the file. It does the opposite of recycle: it prevents the file 
from being deleted and over-written. I think an explicit name like 
move_to_trash is better than something misleading like "recycle".

No need for a special flag to check if the function exists, just check 
for the function:

try:
    f = shutil.send_to_trash
except AttributeError:
    f = os.remove
f(my_file)


But I'm still not sure that this needs to be in the standard library. 
For such a specialist need, what's wrong with using a third party 
solution?


-- 
Steven


More information about the Python-ideas mailing list