2010/07/01

Torrent Files: Begone! (And Be Recoverable!)

Whenever I download a torrent file, the file ends-up in my web browser's download directory. Then I invariably click on the .torrent file to start the download. Since I usually download a few .torrent files a day, my download directory becomes cluttered with files I do not really want. But I do not want to delete the files since I might want to forward them to someone else later or download the rest of a partially downloaded torrent at a later time.

As with any difficulty in live, it can be solved by a small Python script. The best way I found to handle the files is to just move the files in a subdirectory. So I just "double-click" the script whenever I think too many .torrent files are lying around.

import os
import fnmatch

def main():
  try:
    _clear_torrent_files()
  except BaseException as e:
    print(e)
  input("Press a key to exit")

def _clear_torrent_files():
  files = os.listdir('./')

  files_move = []
  for file in files:
    if fnmatch.fnmatch(file, '*.torrent'):
      files_move.append(file)
  
  x=0
  for file in files_move:
    x +=1
    folder_dest = './TorrRep/__Cleared/'
    _createDirIfNotExist(folder_dest)
    file_dest = folder_dest + file
    str = '[{2}] - Moving {0} to {1}'.format(file, file_dest, x)
    if len(str) > 70:
      str = str[0:69]
    print (str)
    os.rename(file, file_dest)

def _createDirIfNotExist(in_dir):
  if not os.path.exists(in_dir):
    os.makedirs(in_dir)

if __name__ == '__main__':
  main()

Then the files are 'gone', but recoverable!

No comments: