Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- """
- Ahh those pesky Thumbs.db files...
- This simple module removes them all from your disk.
- It also exposes a utility function get_file_list()
- that returns the full path of files that matches a list of patterns.
- Written by Caglar Toklu.
- """
- import sys
- import os
- import fnmatch
- def get_file_list(top=".", lst_patterns=None):
- """
- Returns the file list starting from the specified directory top.
- top is a string indicating the directory to start the search.
- lst_patterns is a list of strings indicating patterns to select files.
- Required imports:
- import sys
- import os
- import fnmatch
- """
- if lst_patterns == None:
- lst_patterns = ["*"]
- lst_files_selected = []
- for root, dirs, files in os.walk(top, topdown=False):
- # The following exists only to use 'dirs' and make Pylint happy.
- # It has absolutely no effect.
- sys.stdout.write(str(type(dirs))[:0])
- for file_name in files:
- full_file_name = os.path.join(root, file_name)
- matched = False
- for a_pattern in lst_patterns:
- if fnmatch.fnmatch(file_name, a_pattern):
- matched = True
- break
- if matched:
- lst_files_selected.append(os.path.normpath(full_file_name))
- return lst_files_selected
- def delete_thumbs_db_files():
- """
- Deletes all the thumbs.db files from your disk.
- """
- lst_patterns = ["thumbs.db"]
- print "Building file list, please wait..."
- lst_files_selected = get_file_list("/", lst_patterns)
- print len(lst_files_selected), "found."
- deleted = 0
- failed = 0
- for full_file_name in lst_files_selected:
- try:
- os.remove(full_file_name)
- deleted = deleted + 1
- print "OK:", full_file_name
- except WindowsError:
- print "FAIL:", full_file_name
- failed = failed + 1
- except:
- print "FAIL:", full_file_name
- failed = failed + 1
- print deleted, "file(s) successfully deleted", failed, "failed."
- def main():
- """
- The entry point of the module.
- """
- delete_thumbs_db_files()
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement