Skip to content

Instantly share code, notes, and snippets.

@mitsuhisaT
Created March 24, 2020 12:54
Show Gist options
  • Save mitsuhisaT/995bb0be488008efe5a2f83beea81cc8 to your computer and use it in GitHub Desktop.
Save mitsuhisaT/995bb0be488008efe5a2f83beea81cc8 to your computer and use it in GitHub Desktop.
rename many directories by Python via Regular expression and Pathlib.
#! /usr/bin/env python3
import os
import re
from pathlib import Path
for dn, dps, fns in os.walk('.'):
if re.search(r'\./.+_\d{8}', dn) is not None:
# rename to 20200324_test from test_20200324
Path(dn).rename(Path(re.sub(r'(\./)(.+)_(\d{8})', r'\1\3_\2', dn)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment