Implement custom separators via argparse
This commit is contained in:
		| @@ -1,18 +1,24 @@ | |||||||
| #!/usr/bin/env python3 | #!/usr/bin/env python3 | ||||||
| from sys import argv |  | ||||||
| from unidecode import unidecode | from unidecode import unidecode | ||||||
| from os import rename | from os import rename | ||||||
| from os.path import basename, dirname | from os.path import basename, dirname | ||||||
| from os.path import join as joinpath | from os.path import join as joinpath | ||||||
|  | from argparse import ArgumentParser | ||||||
|  |  | ||||||
|  | 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).') | ||||||
|  |  | ||||||
|  | args = parser.parse_args() | ||||||
|  |  | ||||||
|  |  | ||||||
| # whitespaces will be replaced | # special characters that will not be removed | ||||||
| # dots are ok | whitelist = {' ', '.', '-'} | ||||||
| # do not ruin previous work | whitelist.add(args.separator) | ||||||
| # dashes are ok |  | ||||||
| whitelist = (' ', '.', '_', '-') |  | ||||||
|  |  | ||||||
| for path in argv[1:]: | for path in args.files: | ||||||
|     dir = dirname(path) |     dir = dirname(path) | ||||||
|     file = basename(path) |     file = basename(path) | ||||||
|     rename(path, |     rename(path, | ||||||
| @@ -21,7 +27,7 @@ for path in argv[1:]: | |||||||
|                joinpath |                joinpath | ||||||
|                ( |                ( | ||||||
|                    dir, |                    dir, | ||||||
|                    ''.join(ch for ch in file.replace(' ', '_') |                    ''.join(ch for ch in file.replace(' ', args.separator) | ||||||
|                            if ch.isalnum() |                            if ch.isalnum() | ||||||
|                            or ch in whitelist) |                            or ch in whitelist) | ||||||
|                ) |                ) | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user