1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
import os import shutil import sys import time from pySan import utils as _utils appName = os.path.splitext(os.path.basename(__file__))[0] # fromLoc = '/Users/sanjeevkumar/Movies/' # loc = '/Volumes/Archives&Backup/Videos/Movies' extDisk = '/Volumes/Iomega_HDD/' # extDisk = '/Volumes/Untitled/' loc = os.path.join(extDisk, 'Movies') fromLoc = loc logFile = os.path.join(fromLoc, '%s.log' % appName) _log = _utils.function_logger(appName, logFile, 'INFO', 'DEBUG', console_level=True) def get_size(start_path = '.'): if os.path.isfile(start_path): return os.path.getsize(start_path) total_size = 0 for dirpath, dirnames, filenames in os.walk(start_path): for f in filenames: fp = os.path.join(dirpath, f) if os.path.exists(fp): total_size += os.path.getsize(fp) return total_size def elapsed_time(start, end): # For use with time.time() values as start and end units = [" days "," hours "," minutes ", " seconds"] delta = end - start sec = "{:.3f}".format(delta % 60) # Format seconds to 3 decimals minutes = str(int(delta // 60 % 60)) hours = str(int(delta // 3600 % 24)) days = str(int(delta // 86400)) time_val = [days,hours,minutes,sec] time_passed = "" for i in range(0,4): if time_val[i] != '0': time_passed = time_passed + time_val[i]+units[i] return time_passed def main(): files = os.listdir(fromLoc) years = tuple(str(index) for index in xrange(1900, 2015) ) for year in years: for phile in files: yearFolder = os.path.join(loc, year) if year in phile and phile not in years: if not os.path.exists(yearFolder): _log.debug("%s folder not found, creating." % year) try: os.mkdir(yearFolder) except OSError as osEr: _log.error("Skipping %s due to %s" % (phile, str(osEr))) continue if phile in os.listdir(yearFolder): _log.debug("%s already exists in %s" % (phile, yearFolder)) continue _from = os.path.join(fromLoc, phile) to = os.path.join(yearFolder, phile) sizeofFolderToMove = get_size(start_path=_from) # do not skip if _from location starts with to location and free disk space is greater than size of file/folder to move if not _from.startswith(to.split(year)[0]) and _utils.disk_usage(path=extDisk).free < sizeofFolderToMove: _log.info("Skipping %s. Not enough space in destination." % phile) continue _log.info("Moving %s %s to %s" % (phile, fromLoc, yearFolder)) try: phile_size = _utils.sizeof(get_size(_from)) startMsg = "Started to move %s of %s." % (phile , ''.join(phile_size)) _log.info(startMsg) start = time.time() if _from.startswith(to.split(year)[0]): os.rename(_from, to) else: shutil.move(_from, to) except (IOError, OSError) as er: erMsg = "%s : Deleting %s from %s" % (str(er), phile , to) _log.error(erMsg) else: end_time = time.time() printTimeTaken = "It took %s to move %s." % (elapsed_time(start, end_time), phile) _log.info(printTimeTaken) if __name__ == '__main__': initMsg = "Starting file move task: %s" % time.strftime('%X %x %Z') _log.info(initMsg) sys.exit(main()) |
Today I wrote a quick script to move collection of movies I have downloaded over period of time, my macbook was getting filled, and my external drives I have them connected to TV were lying untouched, so I plugged in drives to my laptop and moved stuff in year based organised order, I did not read year from the file metadata, since I downloaded movies from internet mostly contains year in the name which was very helpful to put them into right year folder, but I really wished if these movie files have metadata. Here is the script you can use to organise, if you do not want to move and just organise within the same folder just make sure _from and to location are same. In that case it will organise movies based on year of release in the same folder.
It has dependency on my own package pySan, you can grab it from PyPi and keep following this post I will update in near future to update based on genre, resolution , and rating using data from IMDb.
0 Comments
Your comment will be posted after it is approved.
Leave a Reply. |
![]()
Custom Search
Other Blogs & PagesGit Commands Animation & VFX SitesA MUST READ for Ani/VFX Artistsawakened by thoughts,
|