Skip to content

Instantly share code, notes, and snippets.

Created September 10, 2018 13:19
What would you like to do?
Send files to Trash on Mac, imitating shell command rm as closely as possible. Based on and
#!/usr/bin/env python
import os
import sys
import subprocess
if len(sys.argv) > 1:
files = []
for arg in sys.argv[1:]:
if os.path.exists(arg):
p = os.path.abspath(arg).replace('\\', '\\\\').replace('"', '\\"')
files.append('the POSIX file "' + p + '"')
print "rem:", arg + ": No such file or directory"
if len(files) > 0:
cmd = ['osascript', '-e',
'tell app "Finder" to move {' + ', '.join(files) + '} to trash']
sys.exit(, stdout=open(os.devnull, 'w')))
print "usage: rem file(s)"
print " move file(s) to Trash"
Copy link

Use sys.argv[0] instead of rem in this call and I can call it "trash" instead ;-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment