Skip to content

Instantly share code, notes, and snippets.

@PaperNick
Created July 8, 2023 14:44
Show Gist options
  • Save PaperNick/817168972280d0bc45d627e4ec8b848c to your computer and use it in GitHub Desktop.
Save PaperNick/817168972280d0bc45d627e4ec8b848c to your computer and use it in GitHub Desktop.
Rename all files and directories - Safe for Windows
from pathlib import Path
special_chars = ('<', '>', ':', '"', '/', '\\', '|', '?', '*', '/')
def replace_all(text, items):
for i in items:
text = text.replace(i, '')
return text
print('WARNING: Scaning large directories trees may take a lot of time.')
for file in Path('.').glob('**/*'):
if any(map(lambda char: char in file.name, special_chars)):
new_name = replace_all(file.name, special_chars)
print(f'Old name: {file}')
print(f'New name: {file.with_name(new_name)}')
print()
file.rename(file.with_name(new_name))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment