Skip to content

Instantly share code, notes, and snippets.

@devilholk
Created October 12, 2019 22:43
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 devilholk/f70ba27ee2b5e9b3dc4b1222ae3c4c9e to your computer and use it in GitHub Desktop.
Save devilholk/f70ba27ee2b5e9b3dc4b1222ae3c4c9e to your computer and use it in GitHub Desktop.
Renames spaces to underscore in current working directory
import os, argparse
parser = argparse.ArgumentParser()
parser.add_argument("--dry-run", help="Just say what we would do", action="store_true")
settings = parser.parse_args()
for filename in os.listdir(os.getcwd()):
if ' ' in filename:
new_filename = filename.replace(' ', '_')
if settings.dry_run:
print(f'Rename `{filename}´ → `{new_filename}´')
else:
try:
os.rename(filename, new_filename)
print(f'Renamed `{filename}´ → `{new_filename}´')
except Exception as e:
print(f'Failed to rename `{filename}´ → `{new_filename}´ due to {e}')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment