Created
January 5, 2021 20:27
-
-
Save Cellebyte/95298bbcf24b75e36481014fc6fbc5de to your computer and use it in GitHub Desktop.
A simple script which allows to rename file by pattern and substitution.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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