Send2Trash

repository·master·Indexed 43 days ago

https://github.com/arsenetar/send2trash

A cross-platform Python package and CLI tool for sending files and directories to the system's native Trash or Recycle Bin on macOS, Windows, and Linux. Version 2.1.0 utilizes native OS calls via ctypes to ensure correct file handling.

Tokens
747
Snippets
4
Records
5
Agent score
6%

What's inside Send2Trash

  1. Install Send2Trash via pip

    master

    You can install the base package using pip. It uses ctypes to access native libraries, so no compilation is required.

    To install with support for pywin32 (Windows) or pyobjc (macOS) to use native platform calls, use the nativeLib extra.

    # Standard installation
    python -m pip install -U send2trash
    
    # Installation with native library support (recommended for Windows/macOS)
    python -m pip install -U send2trash[nativeLib]
  2. Set up a virtual environment for Send2Trash

    master

    On systems enforcing PEP 668 (like recent Linux distributions), you should install Send2Trash within a virtual environment to avoid conflicts with the system Python.

    # Create the environment
    python -m venv .venv
    
    # Activate on GNU/Linux or macOS
    source .venv/bin/activate
    
    # Activate on Windows
    .venv\Scripts\activate
  3. Use the send2trash function

    master

    Import send2trash from the send2trash module to move files or directories to the system's native Trash or Recycle Bin. The function accepts either a single string path or a list of strings.

    from send2trash import send2trash
    
    # Send a single file to trash
    send2trash('some_file')
    
    # Send multiple files to trash
    send2trash(['some_file1', 'some_file2'])
  4. Handle Send2Trash exceptions

    master

    When using Send2Trash, you should be prepared to handle the following exceptions:

    • send2trash.TrashPermissionError: Raised on Freedesktop platforms (Linux, BSD, etc.) when a file cannot be efficiently trashed. This typically happens when a file is on a different device than the user's home directory and the application lacks permission to create a .Trash or .Trash-$UID directory. This error inherits from PermissionError.
    • OSError: Raised for any other problems encountered during the trashing process.
  5. Use the send2trash CLI to move files to the trash

    master

    The send2trash command-line tool allows you to send one or more files or directories to the system trash instead of permanently deleting them. This works across multiple platforms.

    Usage

    send2trash [options] <files...>

    Arguments

    • files: One or more file or directory paths to be trashed. This is a required positional argument.

    Options

    • -v, --verbose: When enabled, the tool will print a confirmation message for each file successfully trashed: Trashed «<filename>».

    Error Handling

    If a file cannot be trashed (e.g., due to an OSError), the tool will print the error message to stderr and exit with status code 1.

    send2trash file1.txt directory_name/ --verbose