Skip to content

Instantly share code, notes, and snippets.

@Cellebyte
Created January 5, 2021 20:27
Show Gist options
  • Save Cellebyte/95298bbcf24b75e36481014fc6fbc5de to your computer and use it in GitHub Desktop.
Save Cellebyte/95298bbcf24b75e36481014fc6fbc5de to your computer and use it in GitHub Desktop.
A simple script which allows to rename file by pattern and substitution.
import glob
import argparse
import re
import os
parser = argparse.ArgumentParser(description='Rename some files.')
parser.add_argument('--pattern', type=str, default='')
parser.add_argument('--substitution', type=str, default='')
args = parser.parse_args()
pattern = re.compile(args.pattern)
files = glob.glob('**', recursive=True)
files = [filename for filename in files if pattern.match(str(filename)) ]
for filename in files:
new_filename = pattern.sub(args.substitution, filename)
print(filename, '->', new_filename)
os.rename(filename, new_filename)
# ^(.*?)(-configmap)(.*?\.yaml)$/\1\3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment