From 00b39bb3050f5f9acae25e746522ea864a2fdced Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kjist=C3=B3f?= Date: Tue, 20 Dec 2016 14:22:44 +0100 Subject: [PATCH] Fix a bug where the normalised file would be moved. closes #1 --- normalisename.py | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/normalisename.py b/normalisename.py index b942276..1e8cbb9 100644 --- a/normalisename.py +++ b/normalisename.py @@ -1,6 +1,8 @@ from sys import argv -from os import rename from unidecode import unidecode +from os import rename +from os.path import basename, dirname +from os.path import join as joinpath # whitespaces will be replaced @@ -9,12 +11,18 @@ from unidecode import unidecode # dashes are ok whitelist = [' ', '.', '_', '-'] -for file in argv[1:]: - rename(file, +for path in argv[1:]: + dir = dirname(path) + file = basename(path) + rename(path, unidecode ( - ''.join(ch for ch in file - if ch.isalnum() - or ch in whitelist) + joinpath + ( + dir, + ''.join(ch for ch in file + if ch.isalnum() + or ch in whitelist) + ) ) .replace(' ', '_'))