Ok this might look pretty silly,but I feel compelled to delete every Thumbs.db file that I tend to recieve from my friends. The file is generated in windows systems when one views the contents of a directory in "Thumbnail" or "Filmstrip" mode. The idea is to store the settings with regard to thumbnail and filmstrip view, as well as to keep a cache of the thumbnail of image and video files.
Whenever I borrow some pics or movies from my friends' DVDs, etc, these tiny little files also gets copied over. Now these are completely harmless as far as I know, but since they are of no use in Linux, I dont want them in my system.
Now, deleting a particular file inside every directory is a pretty troublesome task. Hence, I wrote myself a neat little python program that searches directories, passed to it as the absolute path, for the file mentioned and prompts for its removal.
Here goes my first ever self-written-useful-program.
#!/usr/bin/env python
import os
import sys
def del_thumbs_db (start_path):
for i in os.listdir (start_path):
if i=="Thumbs.db": # change to whatever file you want to remove
del_file = os.path.join (start_path, i)
print """
FILE : %s
""" % del_file
#comment out next two lines to removing prompting for each file
ch = raw_input("\nDelete the file ? ")
if ch == "y" or ch == "Y":
os.unlink (del_file)
i = os.path.join (start_path, i)
if os.path.isdir (i):
del_thumbs_db (i)
if len (sys.argv) != 2:
print """thumbs needs an argument..!!
Usage: thumbs [absolute path]"""
quit ()
path = os.path.abspath (sys.argv[1])
if os.path.isdir (path):
del_thumbs_db (path)
else:
print "path is not a directory ..."
Usage is pretty simple, save the program in a file (say, delete) and make it executable by
# chmod +x /path/to/delete
then run the program like this,
$ delete /absolute/path/to/directory/
The program starts searching for the file mentioned, from the directory passed as argument and goes on recursively to search in subdirectories.
Now a simple bash script like this one
rm `find /home/user/path -name 'pattern_to_search_for'`
would have been easier and better suitable for this task, however, that way I was never going to really get on with python. Hoping for some python expert to come along and provide some insight on this code.
Your comments are welcome. :)
0 Comments:
Post a Comment