Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I move file into Recycle Bin / trash on different platforms using PyQt4?

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.

like image 627
bialix Avatar asked Sep 02 '10 15:09

bialix


People also ask

What is difference between Recycle Bin folder?

The Recycle Bin acts a 'holding bay' for deleted items, such as files and folders (and even shortcuts!). When you delete a file or folder, it is not deleted from your computer permanently. Instead, Windows 7 places the deleted items into the Recycle Bin. For these files, it's a bit like being in limbo, really.

Which type of files/folders are stored in the Recycle Bin?

The Recycle Bin is a Windows feature that is similar to a folder in that it stores all deleted files and folders.

Where are recycled files stored?

By default, the Recycle Bin should be present in the upper-left corner of your desktop in both Windows 10 and Windows 11. We find this the easiest way to access the Recycle Bin. Find the icon on your desktop, then either select it and press Enter on your keyboard, or double-click or double-tap on it to open the folder.


2 Answers

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") 
like image 88
Virgil Dupras Avatar answered Sep 21 '22 11:09

Virgil Dupras


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.

like image 25
teukkam Avatar answered Sep 22 '22 11:09

teukkam