Send Files To Trash On All Platforms



If you've ever needed to send files to trash in your cross-platform application, you've probably noticed the painful void in your toolkit. It's just not there. You can copy, move, delete files, but you can't send them to the trash. There's no easy solution (until now) and you need to implement platform-specific code for Mac OS X, Windows and Linux.

On Mac OS X, you have to call FSMoveObjectToTrashSync (or its async friend) from CoreServices. But then, either you have to make a C extension for it or you have to call it through PyObjC. If your app is a Qt based app, you might dislike having to add such a big dependency just for a little FSMoveObjectToTrashSync call.

On Windows, a SHFileOperation (in fact, SHFileOperationW if you're not stuck in the last unicode-less decade) is needed. Again, if you're not comfortable with C (or you don't want to touch the win32 API with a ten foot pole), you need a dependency on pywin32 (and Tim Golden's winshell if you want to make your life even easier).

On Linux, it's free-for-all (pun intended... in hindsight). There's no unified API to send files to the trash. The convention seems to be that sending stuff in ~/.Trash is good enough, but then there's always the problem of external volumes (you don't want to copy files from external volumes on your main hard disk when you send them to the trash!) and systems with alternate trash conventions (Ubuntu makes further organization of the files inside the .Trash folder).

In other words, it's a mess. You thought you could code your cute Qt app once and run it everywhere, but that damn trash thing gets in your way. Well, if you use Python, you're in luck because there's a library for that now! Say hello to send2trash. It's a cross-platform library that sends files to trash using native API calls (without external dependencies).

>>> from send2trash import send2trash
>>> send2trash('some_file')