Skip to content

Instantly share code, notes, and snippets.

@h4
Last active April 15, 2019 16:08
Show Gist options
  • Save h4/e0445fa8cd4a88df292af3cf7589b3de to your computer and use it in GitHub Desktop.
Save h4/e0445fa8cd4a88df292af3cf7589b3de to your computer and use it in GitHub Desktop.
GIT rename bunch of files using python
#!/usr/bin/env python3
import subprocess
import time
from os import walk, path, chdir
import re
regex = re.compile(r'([0-9a-f]+)_(\w+)\.py')
date_re = re.compile(r'(\d+)-(\d+)-(\d+) (\d+):(\d+)')
chdir('/Users/h4/Projects/Arrival/components-catalogue/alembic/versions')
for root, dirs, files in walk('/Users/h4/Projects/Arrival/components-catalogue/alembic/versions'):
for fname in files:
if fname.endswith('.py'):
if regex.match(fname):
full_path = path.join(root, fname)
with open(full_path) as fp:
for i, line in enumerate(fp):
if i == 4:
prefix = ''.join(date_re.search(line).groups())
if i > 5:
break
newname = prefix + '_' + regex.sub(r'\2_\1.py', fname)
subprocess.run(["git", "mv", fname, newname])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment