Skip to content

Instantly share code, notes, and snippets.

@Sleepingwell
Last active January 29, 2024 12:36
Show Gist options
  • Save Sleepingwell/6070119 to your computer and use it in GitHub Desktop.
Save Sleepingwell/6070119 to your computer and use it in GitHub Desktop.
An example of how to copy or move a bunch of files matching a specific regex with python.
# Copyright (C) 2013 Simon Knapp
#
# This program is free software; you can redistribute it and/or
# modify it under the any terms you wish.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# To run this, type:
#
# python move_files.py <move|copy> <input directory> <output directory> [regexp]
#
# at a command prompt. If the first argument is move, then files will be moved
# otherwise files will be copied. It doesn't matter what directory structure
# is found under the input directory, the entire directory tree is walked and
# every file in every sub-directory will be checked.
#
# If no regular expression (4th argument) is provided, the one shown is used, but note that
# the named paramters must be suitable for the constructor of File.
import re, os, sys, shutil
def join(*args):
return os.path.normpath(os.path.join(*args))
class File(object):
def __init__(self, matchObj, directory, fileName):
year = matchObj.group('year')
post = matchObj.group('post')
self.fileName = join(directory, fileName)
self.newName = '.'.join(('_'.join((matchObj.group('pref'), year, matchObj.group('day'))), post, 'hdf'))
self.newDir = join(post, year)
def tryMakeDir(path):
try:
os.makedirs(path)
except:
if not os.path.exists(path):
raise Exception('failed to create output directory: ' + path)
def doMoveOrCopy(inputDir, outputDir, pattern, mover):
prog = re.compile(pattern)
for root, dirs, files in os.walk(inputDir):
for fn in files:
m = prog.match(fn)
if m:
f = File(m, root, fn)
innerOutputDir = join(outputDir, f.newDir)
try:
tryMakeDir(innerOutputDir)
except Exception, e:
print e
else:
mover(f.fileName, join(innerOutputDir, f.newName))
if __name__ == '__main__':
fileNameRegEx = '(?P<pref>[^.]+?)\\.A(?P<year>\d{4})(?P<day>\d{3})\\.(?P<post>h\d{2}v\d{2})'
copyOrMove = sys.argv[1]
inputDir = sys.argv[2]
outputDir = sys.argv[3]
if len(sys.argv) > 4:
fileNameRegEx = sys.argv[4]
doMoveOrCopy(inputDir, outputDir, fileNameRegEx, shutil.copy if copyOrMove == 'copy' else shutil.move)
# the folowing lines should just print a bunch of not able to move messages to the console. unless
# you have a drive named Purple!
#outputDir = 'Purple:/is/a/colour'
#doMoveOrCopy(inputDir, outputDir, fileNameRegEx, shutil.copy if copyOrMove == 'copy' else shutil.move)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment