-
Notifications
You must be signed in to change notification settings - Fork 1
/
filename-fix.py
38 lines (31 loc) · 1010 Bytes
/
filename-fix.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#!/usr/bin/env python3
import os
import sys
REPLACEMENTS = {}
for c in 'öäåÖÄÅ':
REPLACEMENTS[c.encode('utf-8').decode('latin-1')] = c
def fix_path(path):
for r in REPLACEMENTS:
path = path.replace(r, REPLACEMENTS[r])
return path
def rename_path(path, subdir):
new_path = fix_path(path)
if new_path != path:
user_input = input('{} --> {} (Y/n): '.format(path, new_path))
if user_input == '' or user_input == 'Y':
src = os.path.join(subdir, path)
dst = os.path.join(subdir, new_path)
try:
os.rename(src, dst)
except FileExistsError:
if input('{} exists. Continue? (Y/n)'.format(dst)) in ['', 'Y']:
pass
else:
sys.exit()
def main():
for subdir, dirs, files in os.walk(sys.argv[1]):
print(subdir)
for path in files + dirs:
rename_path(path, subdir)
if __name__ == '__main__':
main()