Skip to content

Instantly share code, notes, and snippets.

@binh-bk
Last active June 7, 2018 04:04
Show Gist options
  • Save binh-bk/350c1b3828fa9996f4542449aee043e8 to your computer and use it in GitHub Desktop.
Save binh-bk/350c1b3828fa9996f4542449aee043e8 to your computer and use it in GitHub Desktop.
python3 to look for a list of file format (movies, images) or file name patterns, and move found files into a designated folder, log record
#! /usr/bin/python3
# this script run on Linux, change path in accordance with your OS
import shutil
import fnmatch
import os
# images = ['*.jpg', '*.jpeg', '*.png', '*.tif', '*.tiff']
movies = ['*.mov', '*.mp4', '*.avi', '*.flv', '*.wmv','*.MOV', '*.MP4', '*.AVI', '*.FLV', '*.WMV']
#patterns = ['*_????_1.*'] # regex looking for my duplicated files
rootFolder = '/path/to/your/collection/folder'
mvFolder = '/path/to/your/designated/folder'
matches = []
print('Starting...')
_total_size = 0
for root, dirnames, filenames in os.walk(rootFolder):
for instance in movies: # change moviesto list of patterns or images
for filename in fnmatch.filter(filenames, instance):
# print("working on: {}".format(filenames)) # uncomment for monitoring
_path = os.path.join(root, filename)
_size = os.path.getsize(_path) # >> 20
_total_size += _size
matches.append(_path + '\t' + str(_size))
_new_path = os.path.join(mvFolder, filename)
shutil.move(_path, _new_path) # comment out for dry run
print('Moving {} to {}'.format(filename, _new_path))
# print(matches)
with open(os.path.join(mvFolder, 'log.txt'), 'w') as f: #change log file as desired
for item in matches:
f.write(item+'\n')
f.write("Total found size: {0:.2f} MB".format(_total_size/1024/1024))
print('Done.')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment