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

Andrew Barnert abarnert at yahoo.com
Wed Dec 31 00:12:01 CET 2014

On Dec 30, 2014, at 22:36, Ronald Oussoren <ronaldoussoren at mac.com> wrote:

>> On 30 Dec 2014, at 00:19, Greg Ewing <greg.ewing at canterbury.ac.nz> wrote:
>> Andrew Barnert wrote:
>>> you just look up the name of the trash directory (which I believe
>>> is theoretically localized but actually always ~/.Trash on the drive with
>>> your user directory, $mnt/.Trash otherwise) and call rename.
>> You may also need to invent a unique name if there's already
>> a file in the trash with the same name.

At this point too much context has been stripped from the message for the replies to make sense. The original message was pointing out that there is no separate "low-level API" for trashing that could go in os along with a "high-level API" in shutil--unless you consider rename a low-level API for trashing, in which case it's already there.

> And whatever else the api does for you… IMHO its better to use the API for this on OSX, such as FSMoveObjectToTrashSync.

Except that API is deprecated (meaning, among other things, that if shutil linked to it, any py2app-ed or embedding app that imports shutil can't be sold in the Mac App Store), and there are no suitable replacements.

By "suitable" I mean "suitable for widespread-enough use that it should be in the stdlib". As I said earlier in the thread, I think it's perfectly reasonable to have a third-party library that uses a deprecated API, requires 10.8, requires a runloop, directly or indirectly asks the Finder to do the work for you, or doesn't quite meet Apple's HIG rules, because all of those could be acceptable in _many_ applications; I just don't think any of those should be in the stdlib, because none of those are acceptable in _most_ applications.

If I were designing an app right now, here's what I'd do:

If you don't need 10.7 or earlier, use NSFileManager (via PyObjC if possible, via a tiny custom (Obj)C extension module if for some reason it's not, and same goes for all the following).

If you need 10.5-10.7 but don't care about the App Store, check NSFileManager dynamically, and fall back to the FS function if the method is missing.

If you need 10.6-10.7 and also need the App Store, you've probably got a runloop, so use the async function in NSWorkspace (recycleURLs:completionHandler:).

If you need 10.6-10.7 in a helper app in the App Store, use the other function in NSWorkspace (performFileOperation:source:destination:files:tag:) and deal with all of its problems because you have no choice in the matter.

More information about the Python-ideas mailing list