Advertisement
Guest User

mover.py

a guest
Feb 3rd, 2014
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.68 KB | None | 0 0
  1. ### -*- coding: utf-8 -*-
  2. '''moverrr.py - files autosorter script
  3. - Moves EVERY file in dir with corresponding file's type name except files,
  4.  listed in ex() and files with no extensions
  5. '''
  6. __version__ = "1.0.2r"
  7. __date__ = "13.04.2012"
  8. _news_ = \
  9. '''-----
  10. TODO:Change code to better fit into funcional programming idiom
  11. NEW: Files with unicode name now sorting properly
  12. NEW: Improved code readability
  13. NEW: Porting to Python 3.x
  14. NEW: Renamed to moverrr
  15. '''
  16.  
  17. import os, sys, locale, codecs
  18. from shutil import move
  19.  
  20. sys.stdout = sys.stdout.detach()
  21. sys.stdout = codecs.getwriter('cp866')(sys.stdout, errors='replace')
  22. ##sys.stdout = codecs.getwriter('utf-8')(sys.stdout, errors='replace')
  23.  
  24. ex=("py","pyc","pyw","ini","ico","bat","exe",
  25.     "lnk","DMF","DFMr","download","sys")
  26.  
  27. def main(path):
  28. ##    print('*0*', sys.stdout)
  29.     count=0
  30.     whitelist=[]       #белый список стал локальным - v1.0.2k
  31.     if path:
  32.        print("*1* Directory to sort:", path)
  33.     else:
  34.        path=os.getcwd()
  35.        print("Directory to sort:", path)
  36.     #detecting and checking files in  directory:
  37.     filelist = [e for e in os.listdir(path) if os.path.isfile(e) and not e.endswith(ex)]
  38.     for file in filelist:
  39.        if file.rfind('.')!=-1:
  40.           whitelist.append(file)
  41.           count+=1
  42.        elif file.rfind('.')==-1:
  43.           print('no extension:', file)
  44.           pass
  45.        else:
  46.           print('unknown error with', file)
  47.           pass
  48.     if whitelist: print('white list:', whitelist)
  49.     else:  print('white list is empty!')
  50.     moveit(whitelist, count)
  51.  
  52. def moveit(wlist,count):
  53.     moved=0
  54.     ext=''
  55.     for file in wlist:
  56.          ext=file[file.rfind('.')+1:]
  57.          print('\'.%s\' file detected' % ext)
  58.          #create dir with file-type name:
  59.          try: os.mkdir(".\\%s\\" % ext)
  60.          except OSError: pass
  61.          #moving files into created directory
  62.          if not os.path.exists(".\\%s\\%s" % (ext, file)): #если файла в директории нет,
  63.                move(file, ".\\%s\\" % ext)                 #то перемещаем текущий файл
  64.                moved+=1
  65.                print('\'%s\' moved into \'%s\'' % (file, ext))
  66.          else: print('skipped: \'%s\' already exists in \'%s\'' % (file, ext))
  67.     if count:
  68.        print('-'*32)
  69.        print('%d of %d files processed' % (moved, count))
  70.     else: print("nothing to move! :'<")
  71.  
  72. if __name__ == "__main__":
  73.     print('-'*32)
  74.     print(
  75. '''Auto files sorter v%s by crad, %s
  76. I like to move it move it! :D
  77. %s''' % (__version__, __date__, _news_))
  78.     print('black list:',ex)
  79.     main(sys.argv[1:])
  80.     # main("e:\\downloads\\")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement