Skip to content

Instantly share code, notes, and snippets.

@adamfranco
Last active March 20, 2026 15:41
Show Gist options
  • Select an option

  • Save adamfranco/1530f01642fb3f880a88f48c350aa9ef to your computer and use it in GitHub Desktop.

Select an option

Save adamfranco/1530f01642fb3f880a88f48c350aa9ef to your computer and use it in GitHub Desktop.
Command line Python script to rename table definition files to use a consistent name based on the schema and table name.
#!/usr/bin/env python
import os
import argparse
from pathlib import Path
import re
import chardet
parser = argparse.ArgumentParser(
prog='rename_files',
description='Rename SQL files',
epilog='Text at the bottom of help')
parser.add_argument('-u', '--uppercase', help='Make filenames uppercase instead of lowercase', action='store_true')
parser.add_argument('path')
args = parser.parse_args()
path = Path(args.path)
for f in path.iterdir():
if f.is_file():
with open(f, 'rb') as fBinary:
result = chardet.detect(fBinary.read())
encoding = result['encoding']
print(f"Checking {f.name} ({encoding}) for rename: ")
content = f.read_text(encoding)
# Table renames.
m = re.search(r'CREATE TABLE\s*(\w+\.\w+)\s*\(', content, flags=re.MULTILINE)
if m:
if args.uppercase:
tableName = m[1].upper()
else:
tableName = m[1].lower()
newName = f"{tableName}.create.sql"
if f.name != newName:
f.rename(Path(f.parent, newName))
print(f" Renamed to {newName}")
else:
print(f" nothing to do")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment