Source Code for Me (s-c.me)

Allows you to paste souce code to blogs! Adapted for Twitter! Here is Search Form in case you missed your code.
Tags: Python,image_sort, Created At: 10/24/2016 8:25:32 PMViews:

HTML view:
Copy Source | Copy HTML
  1. #!/usr/bin/env python
  2. # -*- coding: UTF-8 -*-
  3. """ sorts images by resolution"""
  4.  
  5.  
  6. import os,sys
  7. import time
  8.  
  9. dirname = os.path.abspath(sys.argv[1])
  10. try:
  11.     newdir = os.path.abspath(sys.argv[2])
  12. except:
  13.     newdir = dirname
  14.  
  15.  
  16. def image_sort(dirname, newdir, recur= 0):
  17.     if not recur:print 'sorting started ...'
  18.     else: print 'sorting started in %s...'%dirname
  19.     if not newdir: newdir = dirname
  20.  
  21.     imagelist = []
  22.  
  23.     if os.path.isdir(dirname):
  24.         for x in os.listdir(dirname):
  25.             absx = dirname+os.sep+x
  26.             if os.path.isfile(absx):imagelist.append(absx)
  27.             else:
  28.                 image_sort(absx, newdir+os.sep+x,recur=1)
  29.         for name in imagelist:
  30.             try:
  31.                 file_date = time.localtime(os.stat(name).st_mtime)
  32.             except EnvironmentError, error:
  33.                 print 'seems error: %s with '%error, name, '/n'
  34.                 continue
  35.             imdir = '%s--%s--%s'%(file_date.tm_year,file_date.tm_mon,file_date.tm_mday)
  36.             imdir = os.path.join(newdir,imdir)
  37.             if os.path.split(dirname)[-1] == os.path.split(imdir)[-1]:
  38.                 continue
  39.             elif not os.path.exists(imdir):
  40.                 print 'making dir %s'%imdir
  41.                 os.mkdir(imdir)
  42.             try:
  43.                 os.system('move "%s" "%s"'%(name,imdir))
  44.             except EnvironmentError:
  45.                 print 'error with '+ name, '/n'
  46.     if not recur:print 'sorting completed!'
  47.  
  48. if __name__ == '__main__':
  49.     image_sort(dirname, newdir)

Based on Manoli.Net's CodeFormatter. Made by Topbot (c) 2008-2018