35

I would like to add the next feature to my cross-platform PyQt4 application: when user selects some file and select "remove" action on it that file will be moved to Recycle Bin folder instead of being permantly removed. I think I can find Windows-specific solution using Win32 API or something similar, but I'd like to know does similar operation could be executed on Ubuntu/Linux and MaxOSX as well via PyQt4 methods.

bialix
  • 17,856
  • 8
  • 45
  • 61
  • 1
    For those who find this question and are using **PyQt5** rather than PyQt4: In Qt 5.15 the [`QFile.moveToTrash` function](https://doc.qt.io/qt-5/qfile.html#moveToTrash) was added ([announcement](https://doc.qt.io/qt-5/whatsnew515.html#qt-core-module)). I hope this help! – sunyata Feb 20 '21 at 09:23

3 Answers3

74

It's a good thing you're using Python, I created a library to do just that a while ago:

http://www.hardcoded.net/articles/send-files-to-trash-on-all-platforms.htm

On PyPI: Send2Trash

Installation

Using conda:

conda install Send2Trash

Using pip:

pip install Send2Trash

Usage

Delete file or folders

from send2trash import send2trash
send2trash("directory")
Miladiouss
  • 3,532
  • 1
  • 21
  • 30
Virgil Dupras
  • 2,594
  • 20
  • 22
  • New ctypes-based version of your library looks very handy. Thanks – bialix May 08 '11 at 10:03
  • This is awesome. On OS X it works with the 'Put Back' option (when right clicking a file in the trash). Thanks! – Patrick Jan 22 '15 at 19:07
  • 4
    This should be added to the standard library. Does anyone know the proper mechanism for making that sort of proposal? I wrote something up at bugs.python.org, but I don't feel like that's the proper place to do it: http://bugs.python.org/issue24185 – ArtOfWarfare May 14 '15 at 03:39
  • 1
    it also sends folders to trash – MagTun Feb 09 '17 at 12:39
  • conda install Send2Trash – Miladiouss Sep 03 '18 at 08:32
  • 'pip install Send2Trash' raises an alarm from the virus scanner complaining that the package contains a trojan. – Chris Nov 30 '20 at 13:41
2

I guess there really is no cross-platform solution provided by Qt and it's not a totally trivial task to implement the trash concept in Linux since it's slightly different based on which file manager is in use.

Here's a site discussing the trash concept in Nautilus and another one for KDE.

Under Windows you can use the Win32 API like you said. Python solution available here.

Mac OS X puts the trashed files in ~/.Trash similar to other *NIX OSes, but I couldn't quickly Google any documentation for it. It seems that the OS X trash info file is some kind of binary format and not plain text like in Linux.

Symbian doesn't have a desktop concept and thus no trashcan concept either. It might be similar for other mobile platforms.

EDIT: Super User has some discussion revealing that .DS_Store does indeed store information about trashed files, but no specifics about the format.

Community
  • 1
  • 1
teukkam
  • 4,197
  • 1
  • 24
  • 35
1

The best OSX solution I know uses Applescript. I did not, however, invent it, so I shall simply link to it here.

It would be nice to have a module that packaged up the Win32/KDE/OSX solutions into one, i feel, and imported the correct one on demand. Is that how you solved your problem in the end?

dan mackinlay
  • 945
  • 1
  • 9
  • 14