delete_thumbnails.py

 1 #!/usr/bin/python
 2 # delete_thumbnails.py 
 3 #   This Nautilus script attempts to locate and delete thumbnails of selected 
 4 #   files
 5 #
 6 # Copyright 2007 Barak Korren
 7 #
 8 #    This program is free software: you can redistribute it and/or modify
 9 #    it under the terms of the GNU General Public License as published by
10 #    the Free Software Foundation, either version 3 of the License, or
11 #    (at your option) any later version.
12 #
13 #    This program is distributed in the hope that it will be useful,
14 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
15 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 #    GNU General Public License for more details.
17 #
18 #    You should have received a copy of the GNU General Public License
19 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 #
21 # This script is currently not very smart, it simply does a recursive search
22 # in ~/.thumbnails deleting all matching thumbnails it finds, a smarter script
23 # would not recuse into directories in ~/.thumbnails/large and 
24 # ~/.thumbnails/normal since such directories do not contain thumbnails 
25 # according to ths standard.
26 #
27 # The file serch could be optimized as well, curently it does O(n^2) kartezian
28 # duplication scan (e.g. compares each thumbnail name to each file name in each
29 # directory), which could run a little slow on older machines or ones with 
30 # gigantic amounts of thumbnails.
31 #
32 
33 import os
34 import md5
35 
36 try:
37         thumbs = [md5.new(url).hexdigest() + '.png' for url in
38                         os.environ['NAUTILUS_SCRIPT_SELECTED_URIS'].splitlines()]
39         tokill = list()
40         for root, dirs, files in os.walk(os.path.expanduser('~/.thumbnails')):
41                 tokill.extend([os.path.join(root,f)
42                         for t in thumbs for f in files if f == t]);
43         #print tokill
44         for f in tokill:
45                 os.remove(f);
46 except KeyError, e:
47         if e.message == 'NAUTILUS_SCRIPT_SELECTED_URIS':
48                 print 'NAUTILUS_SCRIPT_SELECTED_URIS environment variable not \
49 available! are you running this from Nautilus?'
50                 exit(1)
51         else:
52                 raise
53 
54  

4 thoughts on “delete_thumbnails.py

  1. Pingback: Fixing Thumbnails in Nautilus « Ifblog (ponderings 2.0)

    • I did not post a plain-text version so far for the simple reason that wordpress.com would not let me do so.
      Seeing that you retained all the original copyright notices and comments, I`m rather happy with you keeping a copy on your site.

      • Wow, wordpress.com allows uploading of all kinds of Microsoft formats, but not plain text. How very odd.

        I’m happy to host a plain text version, assuming the bandwidth doesn’t get out of control on this 2.2K file… :-)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s