normalisename/normalisename.py

35 lines
1.1 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
2016-12-08 17:15:56 +00:00
from unidecode import unidecode
from os import rename
from os.path import basename, dirname
from os.path import join as joinpath
from argparse import ArgumentParser
2016-12-08 17:15:56 +00:00
parser = ArgumentParser(description='Eliminate funky stuff from filenames.')
parser.add_argument('--separator', '-s', type=str, nargs='?', default='_',
help='Set separator to use (to replace spaces with).')
parser.add_argument('files', type=str, nargs='+',
help='File(s) to normalise the name of (relative or absolute path).')
2016-12-08 17:15:56 +00:00
args = parser.parse_args()
# special characters that will not be removed
whitelist = {' ', '.', '-'}
whitelist.add(args.separator)
for path in args.files:
dir = dirname(path)
file = basename(path)
rename(path,
2016-12-08 17:15:56 +00:00
unidecode
(
joinpath
(
dir,
''.join(ch for ch in file.replace(' ', args.separator)
if ch.isalnum()
or ch in whitelist)
)
))