Created
October 12, 2019 22:43
-
-
Save devilholk/f70ba27ee2b5e9b3dc4b1222ae3c4c9e to your computer and use it in GitHub Desktop.
Renames spaces to underscore in current working directory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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