Skip to content

Instantly share code, notes, and snippets.

@McFateM
Last active July 22, 2020 18:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save McFateM/4151595b232e3ccaf68d61297243c4bd to your computer and use it in GitHub Desktop.
Save McFateM/4151595b232e3ccaf68d61297243c4bd to your computer and use it in GitHub Desktop.
Used in the Rootstalk project to sanitize filenames
import os
"""
Renames the filenames within the same directory to be Unix friendly
(0) Removes "Volume V, Issue 1, "
(1) Makes lowercase (not a Unix requirement, just looks better ;)
(2) Changes spaces, colons, apostrophes, underscores and exclaimation points to hyphens
(3) Removes commas and semi-colons
(4) Replaces doubled hyphens and doubled periods with singles
(5) Replaces ".-" with "-" and "-." with "."
Usage:
python change-filenames.py
"""
path = os.getcwd()
filenames = os.listdir(path)
for filename in filenames:
os.rename(filename, filename.replace("Volume V, Issue 1, ", "").lower())
for filename in filenames:
os.rename(filename, filename.replace(" ", "-").replace(",", "").replace(";", "").replace(":", "-").replace("'", "-").replace("!", "-").replace("_", "-").replace("--", "-").replace("..", ".").lower())
for filename in filenames:
os.rename(filename, filename.replace("--", "-").replace("..", ".").replace(".-", "-").replace("-.", "."))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment